是否可以将数据作为initializer_list 传递给std::array 结构?

发布于 2024-12-26 16:07:28 字数 1810 浏览 1 评论 0原文

我有以下代码。基本上我想使用聚合初始化语法初始化非 POD 结构的 std::array 。 g++ 4.6 和 4.7(最新的每周快照)都无法编译代码。

#include <array>

struct TheClass {

    int a1, a2;

    TheClass(int b1, int b2) : a1{b1}, a2{b2} {};    
};

template<unsigned D>
struct OtherClass {

    std::array<TheClass, D> a;

    OtherClass(std::array<TheClass, 2> b) : a{b} {};
};

int main()
{
    OtherClass<2>{{ {1, 2}, {2, 3} }}; //tried a lot of options here
}

GCC 错误:

v.cpp: In function ‘int main()’:
v.cpp:20:37: error: no matching function for call to ‘OtherClass<2u>::OtherClass(<brace-enclosed initializer list>)’
v.cpp:20:37: note: candidates are:
v.cpp:15:5: note: OtherClass<D>::OtherClass(std::array<TheClass, 2ul>) [with unsigned int D = 2u]
v.cpp:15:5: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::array<TheClass, 2ul>’
v.cpp:11:8: note: constexpr OtherClass<2u>::OtherClass(const OtherClass<2u>&)
v.cpp:11:8: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const OtherClass<2u>&’
v.cpp:11:8: note: constexpr OtherClass<2u>::OtherClass(OtherClass<2u>&&)
v.cpp:11:8: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘OtherClass<2u>&&’

我的问题:上面的代码正确吗?看起来,由于 std::array 是一个聚合,因此构造其数据成员应该没有问题。也许这是 GCC 中的一个错误?

编辑

OtherClass<2>{{ TheClass{1, 2}, TheClass{2, 3} }}; 当然可以,但我不想使用因为我必须在很多地方构建课程。 C++11 应该支持省略 TheClass。另请参阅这个问题

I have the following code. Basically I want to initialize a std::array of non-POD structs using aggregate initialization syntax. Both g++ 4.6 and 4.7 (latest weekly snapshot) fails to compile the code.

#include <array>

struct TheClass {

    int a1, a2;

    TheClass(int b1, int b2) : a1{b1}, a2{b2} {};    
};

template<unsigned D>
struct OtherClass {

    std::array<TheClass, D> a;

    OtherClass(std::array<TheClass, 2> b) : a{b} {};
};

int main()
{
    OtherClass<2>{{ {1, 2}, {2, 3} }}; //tried a lot of options here
}

GCC errors:

v.cpp: In function ‘int main()’:
v.cpp:20:37: error: no matching function for call to ‘OtherClass<2u>::OtherClass(<brace-enclosed initializer list>)’
v.cpp:20:37: note: candidates are:
v.cpp:15:5: note: OtherClass<D>::OtherClass(std::array<TheClass, 2ul>) [with unsigned int D = 2u]
v.cpp:15:5: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::array<TheClass, 2ul>’
v.cpp:11:8: note: constexpr OtherClass<2u>::OtherClass(const OtherClass<2u>&)
v.cpp:11:8: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const OtherClass<2u>&’
v.cpp:11:8: note: constexpr OtherClass<2u>::OtherClass(OtherClass<2u>&&)
v.cpp:11:8: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘OtherClass<2u>&&’

My questions: is the above code correct? It seems that as std::array is an aggregate, there should be no problems to construct its data members. Maybe it's a bug in GCC?

EDIT:

OtherClass<2>{{ TheClass{1, 2}, TheClass{2, 3} }}; works of course, but I don't want to use that as I have to construct the class in a lot of places. C++11 should support omission of TheClass. Also see this question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

清风疏影 2025-01-02 16:07:28

std::array 是一个包含数组的聚合,因此您需要一对额外的大括号来从常规数组初始化它:

{1,2}               // TheClass
{{1,2},{2,3}}       // TheClass[2]
{{{1,2},{2,3}}}     // std::array<TheClass,2>
{{{{1,2},{2,3}}}}   // OtherClass<2>

但是,似乎旧版本的 GCC仍然在努力解决这个问题:4.5.1 拒绝它,而 4.6.1 接受它。

std::array is an aggregate containing an array, so you need an extra pair of braces to initialise it from a regular array:

{1,2}               // TheClass
{{1,2},{2,3}}       // TheClass[2]
{{{1,2},{2,3}}}     // std::array<TheClass,2>
{{{{1,2},{2,3}}}}   // OtherClass<2>

However, it seems that older versions of GCC still stuggle with this: 4.5.1 rejects it, while 4.6.1 accepts it.

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