std::initializer 列表 global/static 对象的生命周期

Lifetime of a std::initializer list global/static object

std::initializer_list 主要用作 class constructors/functions 参数,以便将列表元素复制到另一个容器中。但是如何使用 std::initializer_list 创建一个全局对象呢?例如:

struct ElemType {
    const char* name;
    bool        flag;
};

std::initializer_list<ElemType> MyGlobalData = { {"One",true}, {"Two",false} };

如果查看 std::initializer_list 模板定义(在 Visual Studio 2017 年签入),它仅包含 2 个数据成员:const _Elem *_First_Last。 这意味着初始化列表数据应该存储在一个自动变量中。 在这种情况下它的生命周期是多少?

在 Visual Studio 2017 年测试的此类示例看起来运行良好。但我怀疑这种行为是否符合最新的C++标准。

定义明确。

[dcl.init.list]/5 An object of type std::initializer_list<E> is constructed from an initializer list as if the implementation generated and materialized (7.4) a prvalue of type "array of N const E", where N is the number of elements in the initializer list.

[dcl.init.list]/6 The array has the same lifetime as any other temporary object (15.2), except that initializing an initializer_list object from the array extends the lifetime of the array exactly like binding a reference to a temporary.

强调我的。