• C++中的trivial解释


    Trivial default constructor

    The 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.

    1)什么时候才会合成出一个default constructor呢?当编译器需要它的时候!此外,被合成出来的constructor只执行编译器所需的行动。
    2)如果已经存在constructors,那么编译器会扩张已存在的constructors,在其中安插一些代码,使得user code被执行之前,先调用必要的default constructors。
    3)如果设计者提供了多个constructors,但没有提供default constructor,这时编译器不会合成一个新的default constructor,因为其他“由user所提供的constructors”存在的缘故。


    Trivial copy constructor

    The 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 destructor

    The 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.

    参考:cppreference.com

    详细请参考:《深度探索C++对象模型》,不过需要注意的是shallow copy = bitwise copy与deep copy = memberwise copy。

  • 相关阅读:
    asp.net mvc异常处理的不同方法
    获取计算机网络信息,包含IP,MAC
    MessageBox页面消息弹出框类
    centos7.4离线安装.NETCore3.1 环境
    .NET Core 3.0 WebApi 使用Swagger
    android隐藏apk方式以及apk之间的启动方式
    react native 更改项目包名
    react native windows下打包apk流程
    CentOS7下安装Redis
    EFCore配置多对多关系
  • 原文地址:https://www.cnblogs.com/shuaihanhungry/p/5764019.html
Copyright © 2020-2023  润新知