正在使用“var{args}”进行初始化C++0x 的新功能,还是仅仅是语法糖?
我正在阅读 C++0x 常见问题 并遇到了详细说明初始值设定项列表的部分。这些例子大多是以下形式的变体:
vector<int> vi = { 1, 2, 3 };
vector<int> vj({1, 2, 3});
// etc.
然而,还列出了以下形式:
vector<int> vk{2};
这种形式出现在常见问题解答中的其他地方,我很好奇它在语义上是否与最初的两种形式不同,或者只是 vk( 的语法糖{x,y,z})
。
I was reading the C++0x faq and came across the section detailing initializer lists. The examples were mostly variations of:
vector<int> vi = { 1, 2, 3 };
vector<int> vj({1, 2, 3});
// etc.
However, also listed was the form:
vector<int> vk{2};
This form appears elsewhere in the faq, and I am curious as to whether it is semantically different from the initial two forms, or just syntactic sugar for vk({x, y, z})
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
({1, 2, 3})
形式直接调用vector
的构造函数,并传递{1, 2, 3 作为第一个参数}
。 您可以传递更多参数如果
vector
没有第一个参数是initializer_list
或可由{1 初始化的其他类型的构造函数, , 2, 3}
(就像另一个容器类),它不起作用。在您的情况下,它可以工作,因为vector
实际上有一个构造函数,其第一个参数是initializer_list
。这就像在普通函数调用中一样如果省略括号,如
vector; vk{1, 2, 3}
,具体含义取决于类。vector
有一个初始化列表构造函数,它是一个第一个参数为initializer_list
类型的构造函数(可以选择对其的引用),以及所有其他具有默认参数的 params。如果该类具有这样的构造函数,则初始值设定项列表将传递给该构造函数。或者,该类可以只是一个聚合(例如 struct A { int a; int b; int c; };,初始化器列表将初始化成员)或具有接受的构造函数3
单独的int
参数。最后,);或者他们将其声明为显式向量(int, int, int);,如果您使用
= { 1, 2, 3 }
形式与省略括号的版本几乎相同(即仅删除=
),除了它禁止使用显式构造函数(即他们将其声明为显式向量(initializer_list= { 1, 2, 3 }
),则会导致错误。The
({1, 2, 3})
form calls the constructors ofvector<int>
directly, and passes as first argument a{1, 2, 3}
. You could have passed more argumentsIf
vector<int>
would not have a constructor whose first parameter is aninitializer_list
or of another type that could be initialized by{1, 2, 3}
(like, another container class), it would not work. In your case it works becausevector<int>
in fact has a constructor whose first parameter is ainitializer_list<int>
. This is just like in normal function callsIf you omit the parentheses, as in
vector<int> vk{1, 2, 3}
, the exact meaning depends on the class. Avector<int>
has an initializer list constructor, which is a constructor with a first parameter of typeinitializer_list<int>
(optionally a reference to it), and all other params with default arguments. If the class has such a constructor, then the initializer list is passed to that constructor. Alternatively the class could simply be an aggregate (likestruct A { int a; int b; int c; };
, the initializer list would then init the members) or have a constructor that accepts3
separateint
arguments.Finally the
= { 1, 2, 3 }
form is almost identical to the version omitting the parentheses (i.e just removing=
), except that it forbids to use explicit constructors (i.e had they declared it asexplicit vector(initializer_list<int>);
or had they declared aexplicit vector(int, int, int);
instead, it would result in an error if you use= { 1, 2, 3 }
).一种是统一初始化,另一种是初始化器列表。它们是两个不同的东西,尽管如您所见,它们可以产生相似的语法。
是统一初始化 - 其他两个是初始化列表。
One is uniform initialization, and the other is initializer lists. They are two different things, although as you can see, they can produce similar syntax.
is a uniform initialization- the other two are initializer lists.
统一初始化可防止缩小转换范围,即会导致数据丢失的转换:
The uniform initialization prevents narrowing conversions i.e. conversions that would cause loss of data: