How to let C++ compiler raise a warning or error when a const is assigned to a const reference?

Peng Yu pengyu.ut@gmail.com
Mon Sep 18 14:29:00 GMT 2006


> Please do not cross-post to both gcc-help and gcc.

I don't remember I post the message to both gcc-help and gcc.

> I believe your code is legal code.

> It's not code that does what you want, but it is code that is doing what you told it to do.
>
> Keep in mind, warnings are indicators that the compiler has discovered something suspect -- something that MAY BE an error and is something that people either do often enough (inadvertently) or is generally considered bad programming practice to warrant a warning (perhaps even being a recommended warning by ISO 14882).

I think the compiler should suspect there is something wrong, because
the buggy code assigns the reference of a temp variable to a const
reference. It should be that a const reference be assigned to another
const reference.

> My guess is, the situation you cited does not come up often in practice to have caused one of the GCC developers to put a warning into GCC.

If you doing template metaprogramming, you'll see this situation
sometimes. Adding a warning will help the programmer using this
technique.



More information about the Gcc-help mailing list