代码改变世界

C++中的trivial解释

2016-07-20 10:36  shuaihanhungry  阅读(2636)  评论(0编辑  收藏  举报

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。