初始化程序列表构造函数错误与CRTP

时间:2022-05-08 22:50:36

I'm wetting my feet with C++11 and am really confused why this doesn't work:

我正在用C ++ 11弄湿我的脚,我真的很困惑为什么这不起作用:

template <class T>
struct A {
  size_t size() const { return sizeof(T); }
};

struct B : A<B> {
  int x;
  int y;
};

B var {1, 5};

I'm using gcc 4.8.2 and get an error saying:

我正在使用gcc 4.8.2并收到错误说:

no matching function for call to 'B(<brace-enclosed initializer list>)'

It works just fine when I don't derive from A, so does the derivation somehow change the POD-ness of my struct B?

当我不从A派生时,它的工作正常,因此推导以某种方式改变了我的结构B的POD-ness?

1 个解决方案

#1


Aggregate-initialization requires your type to be an aggregate. An aggregate cannot have base classes:

聚合初始化要求您的类型是聚合。聚合不能有基类:

An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

聚合是一个数组或类(第9条),没有用户提供的构造函数(12.1),没有私有或受保护的非静态数据成员(第11条),没有基类(第10条),没有虚函数(10.3) )。

#1


Aggregate-initialization requires your type to be an aggregate. An aggregate cannot have base classes:

聚合初始化要求您的类型是聚合。聚合不能有基类:

An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

聚合是一个数组或类(第9条),没有用户提供的构造函数(12.1),没有私有或受保护的非静态数据成员(第11条),没有基类(第10条),没有虚函数(10.3) )。