C++中的Trivial 、POD、non-POD和Standard Layout概念
POD types
non-POD types
Standard Layout types
A Formal Definition
Informally, a standard layout class is one without direct or indirect virtual member functions, reference data members or virtual base classes. Formally, a standard-layout class is a class that:
- Has no non-static data members of type non-standard-layout class (or array of such types) or reference. In simpler words, a standard layout class shall not have reference variables as data members or member objects of a non-standard layout class.
- Has no virtual functions and no virtual base classes.
- Has the same access control for all non-static data members.
- Has no non-standard-layout base classes.
- Either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members.
- Has no base classes of the same type as the first non-static data member.
standard-layout 类型的类是指:
- 没有 non-standard-layout类型(或这些类型的数组)和引用的非静态数据成员
没有虚函数和虚基类
- 非静态数据成员的访问控制必须是相同的
- 没有non-standard-layout的基类
在最底层的派生类中没有非静态数据成员,而且在最多有一个基类拥有非静态数据成员,或者没有拥有非静态数据成员
- 相同基类类型的非静态数据成员不能作为第一个成员
C++标准把trivial类型定义如下:
一个拷贝不变(trivially copyable)类型是指:
没有non-trivial 的复制构造函数
没有non-trivial的转移构造函数
- 没有non-trivial的赋值操作符
没有non-trivial的转移赋值操作符
- 有一个trivial的析构函数
更多请参考:
点击(此处)折叠或打开
- 1 // empty classes are trivial
- 2 struct Trivial1 {};
- 3
- 4 // all special members are implicit
- 5 struct Trivial2 {
- 6 int x;
- 7 };
- 8
- 9 struct Trivial3 : Trivial2 { // base class is trivial
- 10 Trivial3() = default; // not a user-provided ctor
- 11 int y;
- 12 };
- 13
- 14 struct Trivial4 {
- 15 public:
- 16 int a;
- 17 private: // no restrictions on access modifiers
- 18 int b;
- 19 };
- 20
- 21 struct Trivial5 {
- 22 Trivial1 a;
- 23 Trivial2 b;
- 24 Trivial3 c;
- 25 Trivial4 d;
- 26 };
- 27
- 28 struct Trivial6 {
- 29 Trivial2 a[23];
- 30 };
- 31
- 32 struct Trivial7 {
- 33 Trivial6 c;
- 34 void f(); // it's okay to have non-virtual functions
- 35 };
- 36
- 37 struct Trivial8 {
- 38 int x;
- 39 static NonTrivial1 y; // no restrictions on static members
- 40 }
- 41
- 42 struct Trivial9 {
- 43 Trivial9() = default; // not user-provided
- 44 // a regular constructor is okay because we still have default ctor
- 45 Trivial9(int x) : x(x) {};
- 46 int x;
- 47 }
- 48
- 49 struct NonTrivial1 : Trivial 3 {
- 50 virtual f(); // virtual members make non-trivial ctors
- 51 }
- 52
- 53 struct NonTrivial2 {
- 54 NonTrivial2() : z(42) {} // user-provided ctor
- 55 int z;
- 56 }
- 57
- 58 struct NonTrivial3 {
- 59 NonTrivial3(); // user-provided ctor
- 60 int w;
- 61 }
- 62 NonTrivial3::NonTrivial3() = default; // defaulted but not on first declaration
- 63 // still counts as user-provided
- 64 struct NonTrivial5 {
- 65 virtual ~NonTrivial5(); // virtual destructors are not trivial
- 66 };
這裏有個幾個例子能讓你彻底明白每個standard-layout類型:
点击(此处)折叠或打开
- 1 // empty classes have standard-layout
- 2 struct StandardLayout1 {};
- 3
- 4 struct StandardLayout2 {
- 5 int x;
- 6 };
- 7
- 8 struct StandardLayout3 {
- 9 private: // both are private, so it's ok
- 10 int x;
- 11 int y;
- 12 };
- 13
- 14 struct StandardLayout4 : StandardLayout1 {
- 15 int x;
- 16 int y;
- 17
- 18 void f(); // perfectly fine to have non-virtual functions
- 19 };
- 20
- 21 struct StandardLayout5 : StandardLayout1 {
- 22 int x;
- 23 StandardLayout1 y; // can have members of base type if they're not the first
- 24 };
- 25
- 26 struct StandardLayout6 : StandardLayout1, StandardLayout5 {
- 27 // can use multiple inheritance as long only
- 28 // one class in the hierarchy has non-static data members
- 29 };
- 30
- 31 struct StandardLayout7 {
- 32 int x;
- 33 int y;
- 34 StandardLayout7(int x, int y) : x(x), y(y) {} // user-provided ctors are ok
- 35 };
- 36
- 37 struct StandardLayout8 {
- 38 public:
- 39 StandardLayout8(int x) : x(x) {} // user-provided ctors are ok
- 40 // ok to have non-static data members and other members with different access
- 41 private:
- 42 int x;
- 43 };
- 44
- 45 struct StandardLayout9 {
- 46 int x;
- 47 static NonStandardLayout1 y; // no restrictions on static members
- 48 };
- 49
- 50 struct NonStandardLayout1 {
- 51 virtual f(); // cannot have virtual functions
- 52 };
- 53
- 54 struct NonStandardLayout2 {
- 55 NonStandardLayout1 X; // has non-standard-layout member
- 56 };
- 57
- 58 struct NonStandardLayout3 : StandardLayout1 {
- 59 StandardLayout1 x; // first member cannot be of the same type as base
- 60 };
- 61
- 62 struct NonStandardLayout4 : StandardLayout3 {
- 63 int z; // more than one class has non-static data members
- 64 };
- 65
- 66 struct NonStandardLayout5 : NonStandardLayout3 {}; // has a non-standard-layout base class
結論:
在新的標准下,很多新類型成为POD類型,而且,就算一個類型不是POD類型,我們也可以分別利用POD類型的特性(只要這個類型是trivial或者standard-layout)。
標准模板塊(STL)在頭文件<type_traits>中定義了對這些類型的檢測:
1 template <typename T> 2 struct std::is_pod; 3 template <typename T> 4 struct std::is_trivial; 5 template <typename T> 6 struct std::is_trivially_copyable; 7 template <typename T> 8 struct std::is_standard_layout;
原文:
阅读(386) | 评论(0) | 转发(1) |
-->
C++中的Trivial 、POD、non-POD和Standard Layout概念的更多相关文章
- (译)Kubernetes中的多容器Pod和Pod内容器间通信
原文:https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/Pave ...
- K8s中的多容器Pod和Pod内容器间通信
容器(Container)常被用来解决比如微服务的单个问题,但在实际场景中,问题的解决往往需要多容器方案.本文会讨论将多个容器整合进单个Kubernetes Pod 中,以及Pod中的容器之间是如何通 ...
- iOS开发中的错误整理,pod文件Install失败
一.之前用黑苹果,编辑pod文件用一下格式 platform :ios, "8.0" pod "MJExtension" pod "MJRefresh ...
- k8s之pod与Pod控制器
k8s中最为重要的基础资源,pod,pod controller,service pod controller类型有多种需要向控制器赋值之后使用: kubectl命令使用 kubectk get no ...
- Kubernetes K8S之通过yaml文件创建Pod与Pod常用字段详解
YAML语法规范:在kubernetes k8s中如何通过yaml文件创建pod,以及pod常用字段详解 YAML 语法规范 K8S 里所有的资源或者配置都可以用 yaml 或 Json 定义.YAM ...
- CocoaPods pod install/pod update更新慢的问题
CocoaPods pod install/pod update 最近使用CocoaPods来添加第三方类库,无论是执行pod install还是pod update都卡在了Analyzing d ...
- kubernetes删除pod,pod一直处于Terminating状态
删除pod,pod一直处于Terminating状态 [root@yxz-cluster01 deploy_yaml]# kubectl get pod -n yunanbao NAME READY ...
- 在Salesforce中可以对某一个Object的Standard Button或Link进行重写
在Salesforce中可以对某一个Object的Standard Button或Link进行重写,来实现我们特定的逻辑过程,比如:在删除某个Object之前要判断该Object的某个Field的状态 ...
- Android中build target,minSdkVersion,targetSdkVersion,maxSdkVersion概念区分
Android中build target,minSdkVersion,targetSdkVersion,maxSdkVersion概念区分 标签: build targetminSdkVersiont ...
随机推荐
- css控制div下图片自适应解决方法:图片不超过最大宽度
我们(特别是像我一样的菜鸟)经常会遇到一个问题——图片自适应.这个问题是很普遍的.在文章区,在论坛,可以这么说:哪儿需要上传图片,哪儿就存在这个问题,而论坛上也不时有人询问.为什么?原因很简单,我们不 ...
- (转)Android 自定义 spinner (背景、字体颜色)
Android 自定义 spinner (背景.字体颜色) (2012-07-04 17:04:44) 1.准备两张图片,并做好9.png 2.在drawable中定义spinner_sele ...
- elasticsearch bulk批量导入 大文件拆分
命令如下: curl -s -XPOST http://localhost:9200/_bulk --data-binary @data.json 如果上传的data.json文件较大,可以将其切分为 ...
- 在Linux 64位系统下使用hugepage
首先,为什么要介绍/使用HugePage? 在步入正题之前,先讲一个非常普遍的数据库性能问题. 众所周知,Oracle数据库使用共享内存(SGA)来管理可以共享的一些资源;比如shared pool中 ...
- 1060 Are They Equal
题意: 给出两个浮点数(最大不超过10^100),以及存储的有效位数,判断这两个数是否相等.如12300和12358.9若存储的有效位数为3,则均表示为0.123*10^5,因此视为相等. 思路:[字 ...
- C#之Application.DoEvents()
Application.DoEvents()的最大作用就是时时响应, 可以看做是个线程的一个封装 private void button1_Click(object sender, EventArgs ...
- python学习(十八) 程序打包
18.1 Distutils基础 18.2 打包 18.2.1 建立存档文件 18.2.2 创建Windows安装程序或RPM包 18.3 编译扩展 18.4 使用py2exe创建可执行程序
- XML注释与Description标签及Java:注解(Annotation)的关系
NET中的规范标准注释(一) -- XML注释标签讲解 一.摘要 .Net允许开发人员在源代码中插入XML注释,这在多人协作开发的时候显得特别有用. C#解析器可以把代码文件中的这些XML标记提取出来 ...
- 缓存数据库redis
什么是Redis? Redis是一个TCP服务器,支持请求/响应协议. 在Redis中,请求通过以下步骤完成: 客户端向服务器发送查询,并从套接字读取,通常以阻塞的方式,用于服务器响应. 服务器处理命 ...
- A Look at the Razor View Engine in ASP.NET MVC
The biggest architectural difference that exists between ASP.NET MVC and ASP.NET Web Forms is the ne ...