首页 > 编程语言 >C++11 可平凡复制类型 (TriviallyCopyable)

C++11 可平凡复制类型 (TriviallyCopyable)

时间:2023-07-24 21:46:22浏览次数:45  
标签:11 trivially copyable value TriviallyCopyable C++ operator trivial class

可平凡复制类型

  1. 标量类型(算数类型/枚举/指针)
  2. 可平凡复制类类型
  3. 可平凡复制类型的数组
  4. cv限定的可平凡复制类型(const/volatile)

Trivially copyable class

A trivially copyable class is a class that

has at least one eligible copy constructor, move constructor, copy assignment operator, or move assignment operator,

  • each eligible copy constructor is trivial
  • each eligible move constructor is trivial
  • each eligible copy assignment operator is trivial
  • each eligible move assignment operator is trivial, and
  • has a non-deleted trivial destructor.

Trivial class

A trivial class is a class that

  • is trivially copyable, and
  • has one or more eligible default constructors such that each is trivial.
#include <iostream>
using namespace std;

class A
{                                      // trivially copyable
    ~A() = default;                    // trivially copyable
    A() {}                             // trivially copyable
    A(const A &) = default;            // trivially copyable
    A(A &&) = default;                 // trivially copyable
    A &operator=(const A &) = default; // trivially copyable
    A &operator=(A &&) = default;      // trivially copyable
};

class B
{
    virtual void foo() = 0; // not trivially copyable
    // ~B() = delete;             // not trivially copyable
    // ~B() {}                    // not trivially copyable
    // B(const B &) {}            // not trivially copyable
    // B(B &&) {}                 // not trivially copyable
    // B &operator=(const B &) {} // not trivially copyable
    // B &operator=(B &&) {}      // not trivially copyable
};

class C : public B
{
};

void TriviallyCopyableTest()
{
    cout << std::is_trivially_copyable<bool>::value << endl;           // trivially copyable
    cout << std::is_trivially_copyable<char>::value << endl;           // trivially copyable
    cout << std::is_trivially_copyable<int>::value << endl;            // trivially copyable
    cout << std::is_trivially_copyable<float>::value << endl;          // trivially copyable
    cout << std::is_trivially_copyable<double>::value << endl;         // trivially copyable
    cout << std::is_trivially_copyable<std::nullptr_t>::value << endl; // trivially copyable
    cout << std::is_trivially_copyable<int *>::value << endl;          // trivially copyable
    cout << std::is_trivially_copyable<A>::value << endl;              // trivially copyable
    cout << std::is_trivially_copyable<A *>::value << endl;            // trivially copyable
    cout << std::is_trivially_copyable<B>::value << endl;              // not trivially copyable
    cout << std::is_trivially_copyable<B *>::value << endl;            // trivially copyable
    cout << std::is_trivially_copyable<C>::value << endl;              // not trivially copyable
}

参考

https://en.cppreference.com/w/cpp/named_req/TriviallyCopyable
https://en.cppreference.com/w/cpp/language/cv
https://en.cppreference.com/w/cpp/named_req/ScalarType
https://en.cppreference.com/w/cpp/language/classes#Trivially_copyable_class

标签:11,trivially,copyable,value,TriviallyCopyable,C++,operator,trivial,class
From: https://www.cnblogs.com/BuzzWeek/p/17578402.html

相关文章

  • Code-OpenSource-JSON for Modern C++ v3.10.5
    Code-OpenSource-JSONforModernC++v3.10.5github.com/nlohmann/jsonhttps://json.nlohmann.me/home/exceptions/#version-historyhttps://json.nlohmann.me/api/macros/json_diagnostics/#extended-diagnostic-messages#defineJSON_DIAGNOSTICS1输出详细信息......
  • HAOI2011 Problem b
    Problemblink做法:莫比乌斯反演。思路:对于给出的\(n\)个询问,每次求有多少个数对\((x,y)\),满足\(a\lex\leb\),\(c\ley\led\),且\(\gcd(x,y)=k\),\(\gcd(x,y)\)函数为\(x\)和\(y\)的最大公约数。我们设\[\operatorname{f}(n)=\sum\limits_{i=1}^x\sum\limit......
  • c++学习经验总结
    1.关于结构体中定义函数在C++中,结构体中定义函数没问题在C中,则不行。会报expectedspecifier-qualifier-listbefore...2.在C++中,结构体与类的区别:在C++中,结构体是一种特殊形态的类。结构体和类的唯一区别就是:结构体和类具有不同的默认访问控制属性。3.C与C++中结构体......
  • C++ bitset
    C++bitset是C++STL库中的一个类,用于存储二进制位的数组,并提供了一些位操作的函数。下面是一些C++bitset的语法:创建一个bitset:可以使用以下语法创建一个bitset:std::bitset<size>bits;//创建一个大小为size的bitset,所有位都被设置为0std::bitset<size>bits(val);//创......
  • AMEYA360:大唐恩智浦的DNB1168单电芯电池管理芯片方案
    暑期来临,很多朋友会选择短途近郊游,那么AMEYA360觉得电车自驾游不失为一种更经济实惠、绿色环保的出行方式。但是很多人会担心高温天气,万一电车突发事故,甚至起火了怎么办?对此担忧,AMEYA360完全理解,毕竟近年来由于高温天引发的电车起火等事故时有发生。电车热失控风险究其原......
  • ABC311
    T1:FirstABC模拟代码实现n=int(input())s=input()A=B=C=Falseforiinrange(n):ifs[i]=='A':A=Trueifs[i]=='B':B=Trueifs[i]=='C':C=TrueifAandBandC:exit(print(i+1))......
  • c++打印类的内存布局
    内存布局默认32位编译下,4字节对齐有虚函数情况下会在内存一开始多一个虚表指针普通函数不占内存空间静态成员不占内存空间1.通过cl命令输出hello.cpp中的类A的内存布局测试#输出指定类型的内存布局clhello.cpp/d1reportSingleClassLayoutA#输出所有类型的内存布局......
  • C# .NET core Avalonia 11.0版本,发布linux记录
    .netcore7.0+centos7.0cetnos目前运行在hyperV虚拟机里 虚拟机部署的注意事项1需要配置网络环境,  确保在同一局域网下如果sftp无法连接ctrl+shift+f2切换到命令行,开启22端口iptables-IINPUT-ptcp--dport22-jACCEPT同时虚拟机网络也要正确配置.......
  • 记录windows、vs2019、c++、cuda环境配置
    原文链接:   (45条消息)CUDA+Windows+VS环境配置_cudavs_哈哈哈哈哈嗝哈哈哈的博客-CSDN博客            VS2019--c++CUDA环境配置与编程实例-知乎(zhihu.com)测试代码:.cu文件中:#include"stdio.h"#include<cuda_runtime.h>#include<devi......
  • 1143. 最长公共子序列
    1143.最长公共子序列转载:https://www.bilibili.com/video/BV1ey4y1d7oD/?spm_id_from=333.337.search-card.all.click&vd_source=46d50b5d646b50dcb2a208d3946b1598......