不能C ++ POD类型有任何构造函数? [英] Can't C++ POD type have any constructor?

查看:173
本文介绍了不能C ++ POD类型有任何构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  struct A 
{
int b;
};

一个const a;

A 是POD,这样初始化。

  A const a = {3}; 

IMHO,看起来像这样的构造函数。

  struct A 
{
int b;

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

但是Clang假设 A 类型。为什么我不能有这样的构造函数?






我修改了问题以表达我的意思。我错误地写了 struct class ,对不起@Johannes混淆:)

解决方案

POD 表示普通数据类型,根据定义不能有用户定义的构造函数。 / p>

POD实际上是一个聚合类型(请参阅下一个引文)。那么什么是聚合? C ++标准在§8.5.1/ 1节中说明,


聚合是数组或类
)与没有用户声明的
构造函数
(12.1),没有私有或
保护非静态数据成员
(第11节),没有基类


并且C ++标准的§9/ 4部分说,


[....] POD结构是聚合类,没有非静态数据
类型非POD结构的成员,
非POD联合(或这种类型的数组)
或引用,并且具有没有用户定义的
复制赋值运算符

用户定义的析构函数
。类似地,
POD-union是一个聚合联合,
没有类型为
的非静态数据成员non-POD-struct,non-POD-union(或
array的这种类型)或引用,并且
具有没有用户定义的副本分配
operator
没有用户定义的
析构函数
。 POD类是一个类
,它是POD-struct或
POD-union。


从中可以清楚地看出,POD类/ struct / union不能具有用户定义的赋值运算符用户定义的析构函数






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


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


阅读此常见问题:什么是POD类型?


I have a class and a const variable.

struct A 
{
    int b;
};

A const a;

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

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?


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

解决方案

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

POD is actually an aggregate type (see the next quotation). So what is aggregate? The C++ Standard says in section §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).

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

[....] 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.

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


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

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.

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

这篇关于不能C ++ POD类型有任何构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆