在 lambda 中复制initilizer_list 是否合法?

问题描述

请考虑这个简化的 C++14 程序:

#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 在这里发出警告:

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; };

而其他编译器接受该程序:https://gcc.godbolt.org/z/PPrsWxbfM

请问是程序格式错误还是GCC警告错误

解决方法

格式良好,没有UB。

auto l12 延长临时数组的生命周期,并使其保持活动状态直到 main 结束。 auto v 和 lambda 的返回值不会扩展任何东西,但只要 l12 活着就不是问题。


但总的来说,我不建议将 std::initializer_list 用于函数参数以外的任何内容,因为存在棘手的生命周期扩展规则。