加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 返回std :: initializer_list在clang

发布时间:2020-12-16 06:02:25 所属栏目:百科 来源:网络整理
导读:参见英文答案 lifetime of a std::initializer_list return value2 考虑这个代码示例: #include initializer_list#include iostreamint main(){ for(auto e: []()-std::initializer_listint{return{1,2,3};}()) std::coutestd::endl; return 0;} 我试图用g(
参见英文答案 > lifetime of a std::initializer_list return value2
考虑这个代码示例:
#include <initializer_list>
#include <iostream>

int main()
{
    for(auto e: []()->std::initializer_list<int>{return{1,2,3};}())
        std::cout<<e<<std::endl;
    return 0;
}

我试图用g(gcc版本4.9.2(Debian 4.9.2-10))编译它)
输出正确.
在clang(Debian clang版本3.5.0-9(标签/ RELEASE_350 / final)(基于LLVM 3.5.0))输出例如:

0
2125673120
32546

其中第一行始终为0,最后两行为“随机”.

这是cl ang还是别的东西?我认为这个代码示例是正确的.

更新:

当lambda函数返回类型是别的(例如std :: vector或std :: array)时,这段代码工作正常.

解决方法

从C 11 8.5.4列表初始化[dcl.init.list]:

5 An object of type std::initializer_list<E> is constructed from an initializer list as if the implementation allocated an array of N elements of type E,where N is the number of elements in the initializer list. Each element of that array is copy-initialized with the corresponding element of the initializer list,and the std::initializer_list<E> object is constructed to refer to that array. If a narrowing conversion is required to initialize any of the elements,the program is ill-formed.

6 The lifetime of the array is the same as that of the initializer_list object.

您的lambda的return语句初始化一个临时std :: initializer_list< int>并返回其副本.这一切都很好,除了其引用的数组的生命期在完整表达式的结尾结束.通过lambda以外的initializer_list访问死亡数组会导致未定义的行为.

initializer_list不是容器,它是对临时容器的引用.如果你试图像一个容器一样使用它,你将会有一个糟糕的时间.

在C 14(引用N4140)第6段中澄清:

6 The array has the same lifetime as any other temporary object (12.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.

通过CWG issue 1290的解决方案.这个澄清使得不可能使用initializer_list作为例如C 11的意图的成员变量.然而,即使在C14中,您的程序也有未定义的行为.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读