是否可以将数据作为initializer_list 传递给std::array 结构?
我有以下代码。基本上我想使用聚合初始化语法初始化非 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
std::array
是一个包含数组的聚合,因此您需要一对额外的大括号来从常规数组初始化它:但是,似乎旧版本的 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:However, it seems that older versions of GCC still stuggle with this: 4.5.1 rejects it, while 4.6.1 accepts it.