This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/51270] constness violation is accepted without any warning but leads to a required function call being eliminated during optimization


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]