https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63606

            Bug ID: 63606
           Summary: Missing a warning for binding a reference member to a
                    stack allocated parameter
           Product: gcc
           Version: 4.8.2
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bcmpinc at hotmail dot com

The code below should produce a warning, as it binds a stack allocated
parameter to a reference member. However, gcc currently does not produce such a
warning.

The code is error prone as it will always result in a dangling reference: the
object being pointed to is destructed when the constructor returns. Similar
buggy code can accidentally be written when one forgets to insert the '&' to
pass-by-reference. Note that the clang compiler does emit a warning, named
-Wdangling-field, for the code below.


struct Bar {
  int a;
};
struct Foo{
  Foo(Bar arg) : bar(arg) {}
  Bar & bar;
};
int main() {
  Bar k;
  Foo oops(k);
  return 0;
}

Reply via email to