0

I've this simple function:

initializer_list<int> f(){return {1,2,3};}

g++ gives a warning saying:

warning: returning temporary initializer_list does not extend the lifetime of the underlying array [-Winit-list-lifetime]

Is there any risk to return an {1, 2, 3}? Thanks for explanations!

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Troskyvs
  • 7,537
  • 7
  • 47
  • 115

1 Answers1

4

An initializer_list behaves like a reference extending lifetime of a temporary (the temporary being the array).

Lifetime extension doesn't apply when returning references, so it doesn't apply here too. The compiler is right, the returned list is always dangling.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207