什么是花括号列表如果不是intializer_list? [英] What Is a Curly-Brace Enclosed List If Not an intializer_list?

查看:226
本文介绍了什么是花括号列表如果不是intializer_list?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里提出了一个问题:涉及非功能代码的 initializer_list返回的终身扩展

  const auto foo = [](const auto& a,const auto& b,const auto& c){return {a,b,c }; }; 

我相信lambda尝试返回 intializer_list (这是坏,不要这样做。)但我有一个评论


这不是一个 initializer_list ,它是一个初始化列表。两个不同的东西。


我只是想,任何时候你做了一个卷曲的列表,你创建一个 intializer_list

解决方案

这里有三个不同但相关的概念:


  1. braced-init-list :与大括号括起来的名单相关联的语法规则

    初始化列表:列表初始化中使用的 braced-init-list 初始化器的名称


  2. std :: initializer_list :封装一个临时数组


一些例子:

  //支持初始化列表和初始化器列表,
//但不创建std :: initializer_list
int a {4};

// a braced-init-list and initializer list,
//创建一个std :: initializer_list
std :: vector b {1,2,3};

// a braced-init-list和初始化器列表,
//不创建std :: initializer_list(聚合初始化)
int c [] = {1, 2,3};

// d是从初始化器列表创建的std :: initializer_list
std :: initializer_list d {1,2,3};

// e is std :: initializer_list< int>
auto e = {4};

// f以前是一个std :: initializer_list< int>,但是现在int N3922
auto f {4};

您可能想要阅读 N3922 ,其中更改了一些涉及 auto 和<$ c $的规则c> std :: initializer_list 。


I asked a question here: Lifetime Extension of a initializer_list return involving the non-functional code:

const auto foo = [](const auto& a, const auto& b, const auto& c) { return {a, b, c}; };

I believed the lambda was trying to return an intializer_list (that's bad, don't do that.) But I got a comment:

It's not an initializer_list, it's an initializer list. Two different things.

I just thought that any time you did a curly-braced list you were creating an intializer_list. If that's not what's happening, what is a list in curly-braces?

解决方案

There are three distinct, but related concepts here:

  1. braced-init-list: The grammatical rule associated with curly-brace-enclosed lists in certain contexts.

  2. Initializer list: The name for the braced-init-list initializer used in list-initialization.

  3. std::initializer_list: A class wrapping a temporary array which is created in some contexts involving braced-init-lists.

Some examples:

//a braced-init-list and initializer list, 
//but doesn't create a std::initializer_list
int a {4}; 

//a braced-init-list and initializer list,
//creates a std::initializer_list
std::vector b {1, 2, 3};

//a braced-init-list and initializer list,
//does not create a std::initializer_list (aggregate initialization)
int c[] = {1, 2, 3};

//d is a std::initializer_list created from an initializer list
std::initializer_list d {1, 2, 3};

//e is std::initializer_list<int>
auto e = { 4 };

//f used to be a std::initializer_list<int>, but is now int after N3922
auto f { 4 };

You might want to read N3922, which changed some of the rules involving auto and std::initializer_list.

这篇关于什么是花括号列表如果不是intializer_list?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆