用“var {args}”初始化C ++ 0x的一个新功能,还是仅仅是语法糖? [英] Is initializing with "var{args}" a new feature of C++0x, or merely syntactic sugar?
问题描述
我在阅读 C ++ 0x faq ,遇到了详细介绍初始化列表的部分。示例主要是以下变体:
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};
这种形式出现在常见问题的其他地方,我很好奇它是否在语义上与初始两种形式,或只是语法糖为 vk({x,y,z})
。
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})
.
推荐答案
一种是统一初始化,另一种是初始化列表。它们是两个不同的东西,虽然你可以看到,它们可以产生类似的语法。
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.
vector<int> vk{2};
是一个统一的初始化 - 其他两个是初始化列表。
is a uniform initialization- the other two are initializer lists.
这篇关于用“var {args}”初始化C ++ 0x的一个新功能,还是仅仅是语法糖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!