C++ default 关键字

在 C++11 标准中引入的 default 关键字,主要用于在类的声明中显式指定编译器生成默认的特殊成员函数实现。这些特殊成员函数包括:

  1. 默认构造函数(default constructor)
  2. 析构函数(destructor)
  3. 拷贝构造函数(copy constructor)
  4. 拷贝赋值运算符(copy assignment operator)
  5. 移动构造函数(move constructor)
  6. 移动赋值运算符(move assignment operator)
class Demo
{
public:
	// 默认构造
	Demo() = default;
	// 拷贝构造
	Demo(const Demo&) = default;
	// 拷贝赋值
	Demo& operator=(const Demo&) = default;
	// 移动构造
	Demo(Demo&&) = default;
	// 移动赋值
	Demo& operator=(Demo&&) = default;
	// 析构函数
	~Demo() = default;
	
	// 以下编译器无法生成默认实现
	// Demo(int) = default;
	// 普通函数
	// void show() = default;
public:
	int m_a;
	int m_b;
};

1. 默认构造

https://en.cppreference.com/w/cpp/language/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 on its first declaration).
  • 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 (or array thereof) has a trivial default constructor.

A trivial default constructor is a constructor that performs no action. All data types compatible with the C language (POD types) are trivially default-constructible.

2. 默认析构

https://en.cppreference.com/w/cpp/language/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.

3. 拷贝构造

https://en.cppreference.com/w/cpp/language/copy_constructor

The copy constructor for class T is trivial if all of the following are true:

  • it is not user-provided (that is, it is implicitly-defined or defaulted);
  • 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;

A trivial copy constructor for a non-union class effectively copies every scalar subobject (including, recursively, subobject of subobjects and so forth) of the argument and performs no other action. However, padding bytes need not be copied, and even the object representations of the copied subobjects need not be the same as long as their values are identical.

TriviallyCopyable objects 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.

4. 拷贝赋值

https://en.cppreference.com/w/cpp/language/copy_assignment

The copy assignment operator for class T is trivial if all of the following is true:

  • it is not user-provided (meaning, it is implicitly-defined or defaulted);
  • T has no virtual member functions;
  • T has no virtual base classes;
  • the copy assignment operator selected for every direct base of T is trivial;
  • the copy assignment operator selected for every non-static class type (or array of class type) member of T is trivial.

A trivial copy assignment operator makes a copy of the object representation as if by std::memmove. All data types compatible with the C language (POD types) are trivially copy-assignable.

5. 移动构造

https://en.cppreference.com/w/cpp/language/move_constructor

The move constructor for class T is trivial if all of the following is true:

  • it is not user-provided (meaning, it is implicitly-defined or defaulted);
  • T has no virtual member functions;
  • T has no virtual base classes;
  • the move constructor selected for every direct base of T is trivial;
  • the move constructor selected for every non-static class type (or array of class type) member of T is trivial.

A trivial move constructor is a constructor that performs the same action as the trivial copy constructor, that is, makes a copy of the object representation as if by std::memmove. All data types compatible with the C language (POD types) are trivially movable.

6. 移动赋值

https://en.cppreference.com/w/cpp/language/move_assignment

The move assignment operator for class T is trivial if all of the following is true:

  • It is not user-provided (meaning, it is implicitly-defined or defaulted);
  • T has no virtual member functions;
  • T has no virtual base classes;
  • the move assignment operator selected for every direct base of T is trivial;
  • the move assignment operator selected for every non-static class type (or array of class type) member of T is trivial.

A trivial move assignment operator performs the same action as the trivial copy assignment operator, that is, makes a copy of the object representation as if by std::memmove. All data types compatible with the C language (POD types) are trivially move-assignable.

未经允许不得转载:一亩三分地 » C++ default 关键字
评论 (0)

3 + 6 =