https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56568
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution|--- |INVALID
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to fuzzyTew from comment #1)
> Here's an example I ran into using a plain int:
> http://coliru.stacked-crooked.com/a/1dfad4e209339aa1
That's undefined behaviour. You create a temporary array with one element, then
return an initializer_list which is bound to that local array, which goes out
of scope.
Your example and the original testcase are equivalent to:
const int& f() {
long i = 0;
return i;
}
which creates a temporary and binds a reference to it, resulting in a dangling
reference.
std::initializer_list is not a container, don't try to do clever things with
it, you will get burnt.