std::array 初始值设定项列表初始化列表中的初始化
尽管我非常喜欢 C++11 中的新功能,但有时我觉得我错过了它的一些微妙之处。
初始化 int 数组工作正常,初始化 Element2 向量工作正常,但初始化 Element2 数组失败。我认为正确的语法应该是未注释的行,但对我来说,所有初始化尝试都没有成功。
#include <array>
#include <vector>
class Element2
{
public:
Element2(unsigned int Input) {}
Element2(Element2 const &Other) {}
};
class Test
{
public:
Test(void) :
Array{{4, 5, 6}},
Array2{4, 5},
//Array3{4, 5, 6}
Array3{{4, 5, 6}}
//Array3{{4}, {5}, {6}}
//Array3{{{4}, {5}, {6}}}
//Array3{Element2{4}, Element2{5}, Element2{6}}
//Array3{{Element2{4}, Element2{5}, Element2{6}}}
//Array3{{{Element2{4}}, {Element2{5}}, {Element2{6}}}}
{}
private:
std::array<int, 3> Array;
std::vector<Element2> Array2;
std::array<Element2, 3> Array3;
};
int main(int argc, char **argv)
{
Test();
return 0;
}
我已经在 MinGW 下的 g++ 4.6.1 和 4.6.2 上尝试过这个。
我应该如何正确地初始化这个数组?是否可以?
Although I much enjoy the new features in C++11, sometimes I feel like I'm missing some of its subtleties.
Initializing the int array works fine, initializing the Element2 vector works fine, but initializing the Element2 array fails. I think the correct syntax should be the uncommented line, but none of the initialization attempts have succeeded for me.
#include <array>
#include <vector>
class Element2
{
public:
Element2(unsigned int Input) {}
Element2(Element2 const &Other) {}
};
class Test
{
public:
Test(void) :
Array{{4, 5, 6}},
Array2{4, 5},
//Array3{4, 5, 6}
Array3{{4, 5, 6}}
//Array3{{4}, {5}, {6}}
//Array3{{{4}, {5}, {6}}}
//Array3{Element2{4}, Element2{5}, Element2{6}}
//Array3{{Element2{4}, Element2{5}, Element2{6}}}
//Array3{{{Element2{4}}, {Element2{5}}, {Element2{6}}}}
{}
private:
std::array<int, 3> Array;
std::vector<Element2> Array2;
std::array<Element2, 3> Array3;
};
int main(int argc, char **argv)
{
Test();
return 0;
}
I've tried this on g++ 4.6.1 and 4.6.2 under MinGW.
How should I correctly go about initializing this array? Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的方法是
Array{{4, 5, 6}}
。使用聚合初始化来初始化成员时不能省略大括号。唯一可以省略大括号的情况是在以下形式的声明中因此,在您的情况下,您必须键入所有大括号:一个用于
std::array
本身,一个用于int数组。对于
Array3
,您的语法也是正确的,因为int
可以隐式转换为Element2
。从剩下的注释中,
Array3{{{4}, {5}, {6}}}
,Array3{{Element2{4}, Element2{5}, Element2{6 }}}
和Array3{{{Element2{4}}, {Element2{5}}, {Element2{6}}}}
也可以工作,但更加冗长。然而,从概念上讲, Array3{{{4}, {5}, {6}}} 在不执行复制省略的实现上产生最少量的临时对象(我猜这是无关紧要的,但仍然很高兴知道),甚至比Array3{{4, 5, 6}}
还要少,因为您对Element2
使用复制列表初始化,而不是复制初始化,这不产生设计的临时中间人。The correct way to go about this is
Array{{4, 5, 6}}
. You cannot omit braces when you initialize a member with aggregate initialization. The only time you can omit braces is in a declaration of the formSo in your case you have to type out all braces: One for the
std::array
itself, and one for theint
array. ForArray3
, your syntax is correct too, sinceint
can be converted toElement2
implicitly.From the remaining commented ones, the
Array3{{{4}, {5}, {6}}}
,Array3{{Element2{4}, Element2{5}, Element2{6}}}
andArray3{{{Element2{4}}, {Element2{5}}, {Element2{6}}}}
work too, but are more wordy. However conceptionally theArray3{{{4}, {5}, {6}}}
one produces the least amount of temporaries on implementations that don't do copy elision (I guess that's irrelevant, but still good to know), even less than theArray3{{4, 5, 6}}
one, because instead of copy initialization you use copy list initialization for yourElement2
, which doesn't produce an intermediary temporary by design.