A
AdlerSam
Hi,
I wonder why the following two lines produce a warning:
class X {};
const X &f() {return X();}
$ g++ -c ref.cpp
ref.cpp: In function ‘const X& f()’:
ref.cpp:2: warning: returning reference to temporary
As far as I understand, a const reference _extends_ the lifetime of a
temporary until the very last reference instance that refers to the
temporary goes out of scope. Thus, where is the problem that justyfies
the warning?
I wonder why the following two lines produce a warning:
class X {};
const X &f() {return X();}
$ g++ -c ref.cpp
ref.cpp: In function ‘const X& f()’:
ref.cpp:2: warning: returning reference to temporary
As far as I understand, a const reference _extends_ the lifetime of a
temporary until the very last reference instance that refers to the
temporary goes out of scope. Thus, where is the problem that justyfies
the warning?