如何使用C ++ 11统一初始化语法? [英] How to use C++11 uniform initialization syntax?

查看:106
本文介绍了如何使用C ++ 11统一初始化语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解何时以及如何在C ++ 11中使用新的统一初始化语法。

例如,我得到:

  std :: string a {hello world}; // OK 
std :: string b {a}; // NOT OK

为什么在第二种情况下不工作?错误是:

 错误:没有匹配的函数调用'std :: basic_string< char> :: basic_string code> g ++(Ubuntu系统)(

>

/ Linaro 4.5.2-8ubuntu4)4.5.2



有了原始数据,我应该使用什么语法?

  int i = 5; 
int i {5};
int i = {5};


解决方案

的编译错误

  // T t {u}; 
std :: string a;
std :: string b {a};

是四件事的组合




  • 草稿直到不久以前说如果 T 有一个初始化列表构造函数( std :: string 有一个,使用 char 元素),初始化器列表作为参数传递。因此,构造函数的参数不是 a ,而是 {a}


  • 草稿说,初始化引用的初始化器列表不是通过直接绑定完成的,而是首先从初始化器列表中的元素构造一个临时的,然后将目标引用绑定到该临时。


  • 草稿说当初始化列表初始化引用时,当引用的初始化不是直接绑定时,转换序列是用户定义的转换序列。


  • 草案说,当考虑类 X 的构造函数时传递初始化列表本身,作为在如上所述的上下文中的重载解析场景中的候选,则当考虑类型对cv X的引用(cv = const / volatile)的第一构造器参数 - 换句话说非常可能是拷贝或移动构造器时,允许用户定义的转换。否则,如果允许这样的转换,您可以始终运行模糊,因为在列表初始化时,您不仅限于一个嵌套的用户定义的转换。




上述所有的组合是没有构造函数可以使用 { a} initializer_list< char> 不匹配,其他使用 string&&& const string& 是禁止的,因为它们需要用户定义的转换将它们的参数绑定到 {a}



请注意,更新的草案更改了第一条规则:他们说如果没有初始化列表构造函数可以接受初始化列表,那么参数列表由所有元素组成初始化列表。使用更新的规则,您的示例代码将正常工作。


I cannot understand when and how to use the new uniform initialization syntax in C++11.
For example, I get this:

std::string a{"hello world"}; // OK
std::string b{a};  // NOT OK

Why does it not work in the second case? The error is:

error: no matching function for call to ‘std::basic_string<char>::basic_string(<brace enclosed initializer list>)’    

with this version of g++ g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2.

And with primitive data, what syntax should I use?

int i = 5;
int i{5};
int i = {5};

解决方案

The compile error for

// T t{u};
std::string a;
std::string b{a};

Is a combination of four things

  • The draft until not long ago said that if T has an initializer list constructor (std::string has one, taking char elements), that the initializer list is passed itself as an argument. So the argument to the constructor(s) is not a, but {a}.

  • The draft says that an initializer list initializing a reference is done not by direct binding, but by first constructing a temporary out of the element in the initializer list, and then binding the target reference to that temporary.

  • The draft says that when initializing a reference by an initializer list, when the initialization of the reference is not direct binding, that the conversion sequence is a user defined conversion sequence.

  • The draft says that when passing the initializer list itself when considering constructors of class X as candidates in an overload resolution scenario in a context like the above, then when considering a first constructor parameter of type "reference to cv X" (cv = const / volatile) - in other words highly likely a copy or move constructor, then no user defined conversions are allowed. Otherwise, if such a conversion would be allowed, you could always run in ambiguities, because with list initialization you are not limited to only one nested user defined conversion.

The combination of all the above is that no constructor can be used to take the {a}. The one using initializer_list<char> does not match, and the others using string&& and const string& are forbidden, because they would necessitate user defined conversions for binding their parameter to the {a}.

Note that more recent draft changed the first rule: They say that if no initializer list constructor can take the initializer list, that then the argument list consists of all the elements of the initializer list. With that updated rule, your example code would work fine.

这篇关于如何使用C ++ 11统一初始化语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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