Search code examples
c++lambdac++14initializer-list

Is it legal to make a copy of initilizer_list in lambda?


Please consider this simplified C++14 program:

#include <vector>
#include <iostream>

int main()
{
    auto l12 = {1,2};
    auto copy = []( auto v ) { return v; };
    std::vector<int> v{ copy( l12 ) };
    std::cout << v[0] << ' ' << v[1] << '\n';
}

GCC here issues the warning:

warning: returning local 'initializer_list' variable 'v' does not extend the lifetime of the underlying array [-Winit-list-lifetime]
    7 |     auto copy = []( auto v ) { return v; };

while other compilers accept the program: https://gcc.godbolt.org/z/PPrsWxbfM

Could you please say whether the program is ill-formed or GCC warning is wrong?


Solution

  • It is well-formed, and there is no UB.

    auto l12 extends the lifetime of the temporary array, and keeps it alive until the end of main. auto v and the return value of the lambda don't extend anything, but it's not a problem as long as l12 is alive.


    But in general, I wouldn't recommend using std::initializer_list for anything other than a function parameter, because of the tricky lifetime extension rules.