一、什么是trivial

说到TrivialType就得提到c++中的TrivialType类型?在c++中有两个要求:
第一,可平凡复制 (TriviallyCopyable)
第二,若该类型是类类型或其数组,则该类拥有一个或多个合格的默认构造函数,均为平凡的。
更进一步表述在std::is_trivial中,它是如下说明的:
“标量类型、有平凡默认构造函数的可平凡复制类,或这些类/类型的数组,可有 cv 限定”
看一下具体的英文表述:

Trivial default constructorThe default constructor for class T is trivial (i.e. performs no action) if all of the following is true:The constructor is not user-provided (i.e., is implicitly-defined or defaulted)
T has no virtual member functions
T has no virtual base classes
T has no non-static members with default initializers.(since C++11)
Every direct base of T has a trivial default constructor
Every non-static member of class type has a trivial default constructor
A trivial default constructor is a constructor that performs no action. Objects with trivial default constructors can be created by using reinterpret_cast on any suitably aligned storage, e.g. on memory allocated with std::malloc. All data types compatible with the C language (POD types) are trivially default-constructible.Trivial copy constructorThe copy constructor for class T is trivial if all of the following is true:it is not user-provided (that is, it is implicitly-defined or defaulted), and if it is defaulted, its signature is the same as implicitly-defined (until C++14);
T has no virtual member functions;
T has no virtual base classes;
the copy constructor selected for every direct base of T is trivial;
the copy constructor selected for every non-static class type (or array of class type) member of T is trivial;
T has no non-static data members of volatile-qualified type.(since C++14)
A trivial copy constructor is a constructor that creates a bytewise copy of the object representation of the argument, and performs no other action. Objects with trivial copy constructors can be copied by copying their object representations manually, e.g. with std::memmove. All data types compatible with the C language (POD types) are trivially copyable.Trivial destructorThe destructor for class T is trivial if all of the following is true:The destructor is not user-provided (meaning, it is either implicitly declared, or explicitly defined as defaulted on its first declaration)
The destructor is not virtual (that is, the base class destructor is not virtual)
All direct base classes have trivial destructors
All non-static data members of class type (or array of class type) have trivial destructors
A trivial destructor is a destructor that performs no action. Objects with trivial destructors don't require a delete-expression and may be disposed of by simply deallocating their storage. All data types compatible with the C language (POD types) are trivially destructible.

那么,trivial到底是什么意思呢?从前面也可以隐约猜测出来,英文原意它是“细小的,不重要的”意思,在c++一般译作平凡的,有平凡的当然就有不平凡的,不符合平凡的条件的都是不平凡的。

二、作用

那么问题来了,c++中搞这个trivial和non-trivial干啥?说白了你看这些定义就明白了,c++中如果有用户自定义的ctor/dtor,那按照c++的标准,就必须调用这些用户自定义的相关函数,那么其中的是否会有一些没有效率的代码,标准是不管的。但是如果编译器能够判断出trivial,那么它就可以直接按照自己觉得怎么快怎么好用来编译处理这些代码,反正trivial下这些代码都是没啥意义的。这样一来,就会在编译阶段进行一轮代码的优化。比如拷贝两个类对象,如果有自定义的拷贝函数,那么就会调用自定义的,否则就可以直接内存拷贝(memcpy)。

三、例程

看一下具体的例子:

#include <iostream>
#include <type_traits>struct A {int m;
};struct B {B() {}
};int main()
{std::cout << std::boolalpha;std::cout << std::is_trivial<A>::value << '\n';std::cout << std::is_trivial<B>::value << '\n';
}

再看一个判断平凡复制的例子:

#include <iostream>
#include <type_traits>struct A {int m;
};struct B {B(const B&) {}
};struct C {virtual void foo();
};int main()
{std::cout << std::boolalpha;std::cout << std::is_trivially_copyable<A>::value << '\n';std::cout << std::is_trivially_copyable<B>::value << '\n';std::cout << std::is_trivially_copyable<C>::value << '\n';
}

例子非常简单,没有什么可解释的。

三、总结

从这一点来看,所有的相关关键点,都需要从背后的设计出发,单纯的理解一个关键字一个函数,往往陷入细节的纠缠中去。有的时候儿,从细节中跳出来,发现问题的根本,就更容易理解这个问题。如果正向推一个问题比较麻烦,可以试着从反向推一下,再不行,从第三方的角度推一下。
方法论很重要。

c++中的trivial相关推荐

  1. C++中的trivial和non-trivial构造/析构/拷贝/赋值函数及POD类型

    在侯捷的<STL源码剖析>里提到trivial和non-trivial及POD类型,相关知识整理如下. trivial意思是无意义,这个trivial和non-trivial是对类的四种函 ...

  2. C++中POD和trival语义

    最近在看STL源码剖析,书中提到trivial destructor和no-trivial destructor,初次见到对trivial这个概念非常陌生,经过查资料,又发现一个陌生的概念POD,实在 ...

  3. GNU Make 使用手册(于凤昌中译版)

    GNU Make 使用手册(中译版) 翻译:于凤昌 GNU make Version 3.79 April 2000 Richard M. Stallman and Roland McGrath 1 ...

  4. c语言随机产生三个大写字母,C语言编写的随机产生四则运算测试题

    题目:编写一个四则运算测试题的程序,要求每道题都要随机产生 解题思路: 1.编写测试题,且为30道,就要用到循环函数,因此想到用for()函数 2.随机产生两个数,就想到用rand()函数. 注:1. ...

  5. 未命名文章图灵奖Yann LeCun团队提出Masked Siamese ConvNets,让Mask策略也能应用于基于ViT的孪生网络,进行自监督学习!

    图灵奖Yann LeCun团队提出Masked Siamese ConvNets,让Mask策略也能应用于基于ViT的孪生网络,进行自监督学习! [写在前面] 自监督学习在各种视觉基准上表现出优于监督 ...

  6. 求职简历撰写要点和模板分享

    .pdf.zip 求职简历撰写要点: 1.条目化.纲要化 2.突出关键信息 2.突出自己的亮点,去掉和弱化非亮点 3.为目标岗位要求而裁剪 4.咬文嚼字,删除任何一个可删除的字 归纳起来4个字:用户体 ...

  7. 【C/C++面经_其他问题】

    1.C++的多态如何实现 2.为什么析构函数一般写成虚函数 3.构造函数能否声明为虚函数或者纯虚函数,析构函数呢? 4.基类的虚函数表存放在内存的什么区,虚表指针vptr的初始化时间 5.模板函数和模 ...

  8. 用Excel格式舍入数字

    Did you know that Excel limits the number of numbers that appear in a cell, in General format? I dis ...

  9. 如何快速修改文件重命名命名_更改Excel命名范围的地址

    如何快速修改文件重命名命名 In Excel, you can give a name to a range of cells, then use that name in a formula, or ...

  10. 突出显示中奖彩票号码

    No, I've never won the lottery, but that's probably because I don't buy tickets! Your odds of winnin ...

最新文章

  1. 三步搞定 opencv 初始环境设定
  2. mysql generic安装_MySQL 5.6 Generic Binary安装与配置
  3. kali中wireshark打开后错误
  4. mysql数据库备份 dump_MySQL数据库备份之mysqldump
  5. 第七章:清楚简洁的英文 --《英语科技写作(文法与修辞原则)》by 方克涛
  6. 开发经验分享_02_解决问题3步走(实战)
  7. react-native ios打包和Android打包
  8. html5 怎么实现展开文字,html5实现滚动文字
  9. ImportError: cannot import name ‘Optional‘
  10. android微信第三方登录怎么通过code获取openid?
  11. 用存储过程实现的分页程序
  12. 【JAVA】java代码实现print2Flash转swf文件,百度文库一样。
  13. java十大排序算法
  14. pytorch下使用LSTM神经网络写诗
  15. android自动生成cardview,CardView
  16. TI单芯片毫米波雷达代码走读(十四)—— 多普勒维(2D)处理之静态杂波滤除
  17. SpringBoot关闭druid的页面和添加密码验证
  18. 科目二:倒车入库考试技巧详细图解
  19. linux 7 开启远程桌面,CentOS 7 安装使用 VNC 远程桌面
  20. knex简单的增删改查

热门文章

  1. c/c++ 二维数组指针参数传递 矩阵计算实例
  2. char[]和char* 输出长度不同
  3. 阿里云服务器安全组放行宝塔端口8888|888|80|443|20|21教程
  4. PADS VX2.8 原理图图页的添加与名称修改的方法
  5. 公司对员工意见和建议的回复
  6. [转载]Oraclenbsp;grantnbsp;revokenbsp;…
  7. C语言基础之小写字母转大写
  8. 天牛须和贪心算法_天牛须算法
  9. HTML - 调用腾讯 QQ 进行客服在线聊天(PC)
  10. sox处理mp3_SOX 音频处理