简体   繁体   English

std :: array初始化列表初始化列表中的初始化

[英]std::array initializer list initialization in initialization list

Although I much enjoy the new features in C++11, sometimes I feel like I'm missing some of its subtleties. 尽管我非常喜欢C ++ 11的新功能,但有时我仍感觉缺少其某些细微之处。

Initializing the int array works fine, initializing the Element2 vector works fine, but initializing the Element2 array fails. 初始化int数组可以正常工作,初始化Element2向量可以正常工作,但是初始化Element2数组失败。 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. 我已经在MinGW的g ++ 4.6.1和4.6.2上尝试过这个。

How should I correctly go about initializing this array? 我应该如何正确初始化该数组? Is it possible? 可能吗?

The correct way to go about this is Array{{4, 5, 6}} . 解决此问题的正确方法是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 form 您唯一可以省略花括号的形式是在以下形式的声明中

T t = { ... }

So in your case you have to type out all braces: One for the std::array itself, and one for the int array. 因此,在您的情况下,您必须输入所有大括号:一个用于std::array本身,另一个用于int数组。 For Array3 , your syntax is correct too, since int can be converted to Element2 implicitly. 对于Array3 ,您的语法也是正确的,因为int可以隐式转换为Element2

From the remaining commented ones, the Array3{{{4}, {5}, {6}}} , Array3{{Element2{4}, Element2{5}, Element2{6}}} and Array3{{{Element2{4}}, {Element2{5}}, {Element2{6}}}} work too, but are more wordy. 在其余的评论中, Array3{{{4}, {5}, {6}}}Array3{{Element2{4}, Element2{5}, Element2{6}}}Array3{{{Element2{4}}, {Element2{5}}, {Element2{6}}}}工作,但比较罗word。 However conceptionally the Array3{{{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 the Array3{{4, 5, 6}} one, because instead of copy initialization you use copy list initialization for your Element2 , which doesn't produce an intermediary temporary by design . 但是从概念Array3{{{4}, {5}, {6}}}会在不执行复制省略的实现上产生最少的临时数量(我想这是无关紧要的,但还是很容易知道),甚至更少而不是Array3{{4, 5, 6}} ,因为您不是使用复制初始化,而是使用Element2复制列表初始化,这在设计上不会产生中间临时对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM