C ++ POD类型不能有任何构造函数吗?

时间:2023-01-07 22:45:48

I have a class and a const variable.

我有一个类和一个const变量。

struct A 
{
    int b;
};

A const a;

The class A is POD and can be initialized like this.

A类是POD,可以像这样初始化。

A const a = { 3 };

IMHO, it looks fine to have a constructor like this.

恕我直言,有这样的构造函数看起来很好。

struct A 
{
    int b;

    A(int newB) : b(newB)
    {
    }
};

But Clang assumes A as non-aggregate type. Why I can't have constructor like that? Or should I do something else?

但Clang认为A是非聚合类型。为什么我不能有这样的构造函数?或者我应该做别的事情?


I modified question to present my original meaning. I had wrote the struct as class by mistake, and sorry for @Johannes about confusing :)

我修改了问题来表达我原来的意思。我错误地把结构写成了类,并且抱歉@Johannes关于混淆:)

3 个解决方案

#1


17  

POD means Plain Old Data type which by definition cannot have user-defined constructor.

POD表示普通旧数据类型,根据定义,它不能具有用户定义的构造函数。

POD is actually an aggregate type (see the next quotation). So what is aggregate? The C++ Standard says in section §8.5.1/1,

POD实际上是一种聚合类型(参见下一个引用)。什么是聚合? C ++标准在第8.5.1 / 1节中说明,

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

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

And section §9/4 from the C++ Standard says,

C ++标准中的第9/4节说,

[....] A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. Similarly, a POD-union is an aggregate union that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. A POD class is a class that is either a POD-struct or a POD-union.

[....] POD-struct是一个聚合类,它没有非POD-struct类型的非静态数据成员,非POD-union(或这类类型的数组)或引用,并且没有用户 - 定义的复制赋值运算符,没有用户定义的析构函数。类似地,POD-union是一个聚合联合,它没有非POD-struct,非POD-union(或这类类型的数组)或引用类型的非静态数据成员,并且没有用户定义的复制赋值运算符并且没有用户定义的析构函数。 POD类是POD结构或POD结合的类。

From this, its also clear that POD class/struct/union though cannot have user-defined assignment operator and user-defined destructor also.

由此,它也清楚POD类/ struct / union虽然也不能拥有用户定义的赋值运算符和用户定义的析构函数。


There are however other types of POD. The section §3.9/10 says,

然而,还有其他类型的POD。 §3.9/ 10节说,

Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to member types (3.9.2), and cv-qualified versions of these types (3.9.3) are collectively called scalar types. Scalar types, POD-struct types, POD-union types (clause 9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively called POD types.

算术类型(3.9.1),枚举类型,指针类型和指向成员类型的指针(3.9.2)以及这些类型的cv限定版本(3.9.3)统称为标量类型。标量类型,POD结构类型,POD联合类型(第9节),此类型的数组和这些类型的cv限定版本(3.9.3)统称为POD类型。

Read this FAQ : What is a "POD type"?

阅读此常见问题解答:什么是“POD类型”?

#2


5  

The class A is POD and can be initialized like this

A类是POD,可以像这样初始化

Sorry, that is wrong. Because b is private, the class is not a POD.

对不起,那是错的。因为b是私有的,所以该类不是POD。

But Clang assumes A as non-aggregate type. Why I can't have constructor like that? Or should I do something else?

但Clang认为A是非聚合类型。为什么我不能有这样的构造函数?或者我应该做别的事情?

This is a limitation of C++ as it exists currently. C++0x will not have this limitation anymore. While in C++0x your type is not a POD either, your initialization will work (assuming that you make that constructor public).

这是目前存在的C ++限制。 C ++ 0x将不再具有此限制。在C ++ 0x中,您的类型也不是POD,您的初始化将起作用(假设您将该构造函数设为公共)。

(Also, I think a better term for you to use here is "aggregate". The requirement for using { ... } is that your class is an aggregate. It doesn't have to be a POD).

(另外,我认为你在这里使用的一个更好的术语是“聚合”。使用{...}的要求是你的类是一个聚合。它不一定是POD)。

#3


2  

The other answers describe the POD rules pretty well. If you want to get a similar initialization style to a constructor for a POD you can use a make_-style function, for example:

其他答案很好地描述了POD规则。如果要为POD的构造函数获取类似的初始化样式,可以使用make_-style函数,例如:

struct A
{
    int i_;
};

A make_A(int i = 0) 
{
    A a = { i }; 
    return a; 
}

now you can get initialized POD instances like:

现在您可以获得初始化的POD实例,例如:

A a = make_A();

#1


17  

POD means Plain Old Data type which by definition cannot have user-defined constructor.

POD表示普通旧数据类型,根据定义,它不能具有用户定义的构造函数。

POD is actually an aggregate type (see the next quotation). So what is aggregate? The C++ Standard says in section §8.5.1/1,

POD实际上是一种聚合类型(参见下一个引用)。什么是聚合? C ++标准在第8.5.1 / 1节中说明,

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

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

And section §9/4 from the C++ Standard says,

C ++标准中的第9/4节说,

[....] A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. Similarly, a POD-union is an aggregate union that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. A POD class is a class that is either a POD-struct or a POD-union.

[....] POD-struct是一个聚合类,它没有非POD-struct类型的非静态数据成员,非POD-union(或这类类型的数组)或引用,并且没有用户 - 定义的复制赋值运算符,没有用户定义的析构函数。类似地,POD-union是一个聚合联合,它没有非POD-struct,非POD-union(或这类类型的数组)或引用类型的非静态数据成员,并且没有用户定义的复制赋值运算符并且没有用户定义的析构函数。 POD类是POD结构或POD结合的类。

From this, its also clear that POD class/struct/union though cannot have user-defined assignment operator and user-defined destructor also.

由此,它也清楚POD类/ struct / union虽然也不能拥有用户定义的赋值运算符和用户定义的析构函数。


There are however other types of POD. The section §3.9/10 says,

然而,还有其他类型的POD。 §3.9/ 10节说,

Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to member types (3.9.2), and cv-qualified versions of these types (3.9.3) are collectively called scalar types. Scalar types, POD-struct types, POD-union types (clause 9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively called POD types.

算术类型(3.9.1),枚举类型,指针类型和指向成员类型的指针(3.9.2)以及这些类型的cv限定版本(3.9.3)统称为标量类型。标量类型,POD结构类型,POD联合类型(第9节),此类型的数组和这些类型的cv限定版本(3.9.3)统称为POD类型。

Read this FAQ : What is a "POD type"?

阅读此常见问题解答:什么是“POD类型”?

#2


5  

The class A is POD and can be initialized like this

A类是POD,可以像这样初始化

Sorry, that is wrong. Because b is private, the class is not a POD.

对不起,那是错的。因为b是私有的,所以该类不是POD。

But Clang assumes A as non-aggregate type. Why I can't have constructor like that? Or should I do something else?

但Clang认为A是非聚合类型。为什么我不能有这样的构造函数?或者我应该做别的事情?

This is a limitation of C++ as it exists currently. C++0x will not have this limitation anymore. While in C++0x your type is not a POD either, your initialization will work (assuming that you make that constructor public).

这是目前存在的C ++限制。 C ++ 0x将不再具有此限制。在C ++ 0x中,您的类型也不是POD,您的初始化将起作用(假设您将该构造函数设为公共)。

(Also, I think a better term for you to use here is "aggregate". The requirement for using { ... } is that your class is an aggregate. It doesn't have to be a POD).

(另外,我认为你在这里使用的一个更好的术语是“聚合”。使用{...}的要求是你的类是一个聚合。它不一定是POD)。

#3


2  

The other answers describe the POD rules pretty well. If you want to get a similar initialization style to a constructor for a POD you can use a make_-style function, for example:

其他答案很好地描述了POD规则。如果要为POD的构造函数获取类似的初始化样式,可以使用make_-style函数,例如:

struct A
{
    int i_;
};

A make_A(int i = 0) 
{
    A a = { i }; 
    return a; 
}

now you can get initialized POD instances like:

现在您可以获得初始化的POD实例,例如:

A a = make_A();