std::array 初始值设定项列表初始化列表中的初始化

发布于 2025-01-02 03:48:16 字数 1171 浏览 1 评论 0原文

尽管我非常喜欢 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来是傀儡 2025-01-09 03:48:16

正确的方法是 Array{{4, 5, 6}}。使用聚合初始化来初始化成员时不能省略大括号。唯一可以省略大括号的情况是在以下形式的声明中

T t = { ... }

因此,在您的情况下,您必须键入所有大括号:一个用于 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 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. For Array3, your syntax is correct too, since int can be converted to Element2 implicitly.

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文