[Bug c++/84587] [8 Regression] Local variable initializer goes out of scope since r247793

jason at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Feb 27 14:09:00 GMT 2018


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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #2 from Jason Merrill <jason at gcc dot gnu.org> ---
The testcase has undefined behavior; the initializer_list array only lives
until the end of the initialization of 'a'.

11.6.4p6: The array has the same lifetime as any other temporary object (15.2),
except that initializing an initializer_list object from the array extends the
lifetime of the array exactly like binding a reference to a temporary.

15.2p6.9: A temporary object bound to a reference parameter in a function call
(8.5.1.2) persists until the completion of the full-expression containing the
call.

The code can be fixed by explicitly extending the initializer_list lifetime,
e.g.

  std::initializer_list<const char> il = {'a', 'b', 'c'};
  ArrayView<const char> a (il);


More information about the Gcc-bugs mailing list