在union中使用`initializer_list` ctor进行结构化?

时间:2012-02-13 12:25:25

标签: c++ c++11 initializer-list unions

我有一个POD结构,但为方便起见,我希望它有std::initializer_list ctor。默认ctor,copy ctor和dtor是隐含的。然而,似乎使用initializer_list ctor取消了结构作为POD的资格,因此它不能在联盟中:

#include<initializer_list>
struct A{
   A(const std::initializer_list<int>&);
};

union{
   A a;
} a;

gcc 4.6 --std = c ++ 0x:

error: use of deleted function ‘<anonymous union>::._0()’
error: ‘<anonymous union>::._0()’ is implicitly deleted because the default definition would be ill-formed:
error: no matching function for call to ‘A::A()’

周围有它吗?它与c ++ 11的无限制联合功能有关吗?

1 个答案:

答案 0 :(得分:-1)

工会本身必须有一个明确的ctor - 感谢this article):

union _u{
   A a;
   _u(){};
} a;