This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/51270] constness violation is accepted without any warning but leads to a required function call being eliminated during optimization
- From: "redi at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 22 Nov 2011 15:47:45 +0000
- Subject: [Bug c++/51270] constness violation is accepted without any warning but leads to a required function call being eliminated during optimization
- Auto-submitted: auto-generated
- References: <bug-51270-4@http.gcc.gnu.org/bugzilla/>
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51270
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-11-22 15:47:45 UTC ---
If you want warnings then you should request them using -Wall etc.
Although doing so at -O2 gives this, which isn't actually very helpful:
e.cpp:67:60: warning: '<anonymous>' is used uninitialized in this function
[-Wuninitialized]
There is no constness error, the argument to recast_reference creates a
temporary of type 'char const*' which binds to a reference-to-const to that
type:
You can rewrite it as:
int*& faulty_compiled_function(char*& val)
{
char const* tmp = pass_through(val);
return recast_reference(tmp);
}
pass_through and recast_reference are not needed, they simply serve to hide the
error in your code, which is that you return a reference to the temporary, and
use it in main() after the temporary has gone out of scope. Therefore the code
is invalid.