This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
c++/986: g++ misses warning for & on temporary
- To: gcc-gnats at gcc dot gnu dot org
- Subject: c++/986: g++ misses warning for & on temporary
- From: wolfgang dot bangerth at iwr dot uni-heidelberg dot de
- Date: 5 Dec 2000 08:06:53 -0000
- Reply-To: wolfgang dot bangerth at iwr dot uni-heidelberg dot de
>Number: 986
>Category: c++
>Synopsis: g++ misses warning for & on temporary
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: change-request
>Submitter-Id: net
>Arrival-Date: Tue Dec 05 00:16:01 PST 2000
>Closed-Date:
>Last-Modified:
>Originator: Wolfgang Bangerth
>Release: unknown-1.0
>Organization:
>Environment:
>Description:
I happened to write the following code:
-----------------------------------------------
struct X { X (int); };
struct Y {
Y ();
const X &x; // note the ampersand
};
Y::Y () : x(1) {}; // creates a temporary and stores address
---------------------------------------------
G++ did not warn about storing the address of the temporary.
Now, I'm not a beginner in C++, so there's no excuse for writing
this code in the beginning, nevertheless it would have been
nice to have been warned.
In particular, note that were the variable X::x not constant,
then g++ would have warned me. In fact, I have never really
understood why the compiler warns when assigning the
address of a temporary to a non-const variable, while not
doing so for constant ones, but in this particular case it
seems as if the warning could be extended to boths kinds,
as the compiler knows that when in an initializer list the
address of the temporary is stored beyond the lifetime of
the temporary, while clearly this knowledge is not available
when calling an arbitrary function with a temporary as
argument.
So: my request is that the warning
xxx.cc:8: initializing non-const `X &' with `int' will use a temporary
be generated in initialization lists when initializing
references irrespective of cv-qualification of the variable.
>How-To-Repeat:
>Fix:
Avoid code that invoces undefined behavious. Be less stupid
in the beginning.
>Release-Note:
>Audit-Trail:
>Unformatted: