[Bug c++/44859] New: missed warning: returning reference to temporary

joachim dot reichel at gmx dot de gcc-bugzilla@gcc.gnu.org
Wed Jul 7 14:52:00 GMT 2010


struct Base1 {};
struct Derived1 : public Base1 {};

const Base1& f1() { Base1 x; return x; }
const Base1& f2() { return Base1(); }
const Base1& f3() { Derived1 x; return x; }
const Base1& f4() { return Derived1(); }

struct Base2 { int m_foo; };
struct Derived2 : public Base2 {};

const Base2& f5() { Base2 x; return x; }
const Base2& f6() { return Base2(); }
const Base2& f7() { Derived2 x; return x; }
const Base2& f8() { return Derived2(); }

g++ emits a warning for all cases (either "reference to local variable ‘x’
returned" or "returning reference to temporary"), except for the last case f8.
Adding -Wall -Wextra does not change anything. Apparently this seems to be
caused by the cast of a temporary (in contrast to the local variable in f7) to
a non-empty base class.

It would be nice if g++ would warn for this case as well.


-- 
           Summary: missed warning: returning reference to temporary
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: joachim dot reichel at gmx dot de
 GCC build triplet: x64_64-unknown-linux-gnu
  GCC host triplet: x64_64-unknown-linux-gnu
GCC target triplet: x64_64-unknown-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44859



More information about the Gcc-bugs mailing list