与初始化列表共享数据是否在标准范围内有效?

Is sharing data with initializer list out-scoped valid in standard?

lst2 = lst Copying or assigning an initializer_list does not copy the elements in the list. After the copy, the original and the copy share the elements.

根据C++ Primer Table 6.1,assign one initializer list会共享数据,但是如果initializer list与另一个out-scoped共享怎么办,例如,下面的代码是a的一部分函数

std::initializer_list<int> lst1;
{
    std::initializer_list<int> lst2 = {1, 2, 3};
    lst1 = lst2;
}

似乎初始化列表可能共享文字的数据,但标准中指定的数组文字的生命周期是多少?这段代码安全吗?

此代码似乎无效。这是 cppreference

的一些相关摘录

The underlying array is a temporary array of type const T[N], in which each element is copy-initialized (except that narrowing conversions are invalid) from the corresponding element of the original initializer list. The lifetime of the underlying array is the same as any other temporary object, except that initializing an initializer_list object from the array extends the lifetime of the array exactly like binding a reference to a temporary (with the same exceptions, such as for initializing a non-static class member).

退出内部作用域后,临时底层数组将被销毁 - 随着内部 lst2 的生命周期结束 - 封闭作用域中的 lst1 将以悬挂指针结束。