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]

c++/986: g++ misses warning for & on temporary



>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:

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