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

            Bug ID: 102530
           Summary: Warn about non-extended temporary passed to a function
                    returning a reference in temp-extending context
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jason at gcc dot gnu.org
  Target Milestone: ---

struct A {
  ~A();
};
struct B
{
  A a;
  A& get_a() { return a; }
};

int main()
{
  A& ar = B().get_a();
  // ar now refers to a destroyed object
}

We can recognize this situation based on

1) We're initializing a reference (a temporary-extending context) from a
function returning a reference.
2) The arguments to this reference-returning function include a temporary with
non-trivial destruction (or, recursively, another ref-returning function with
such an argument).

Reply via email to