Constexpr Implicitly Declared Functions

For a class of type T, the following compilers can be generated by the compiler depending on the class:

  • default constructor: T::T()
  • copy constructor: T::T(const T&)
  • move constructor: T::T(T&&)
  • Copy Assignment Operator: T& T::operator=(const T&) Assignment Operator
  • : T& T::operator=(T&&)

In C ++ 14 and C ++ 17, what are the rules leading to the generation of constexpr versions of these functions by the compiler?

+5
source share
1 answer

The rule is simple: if the generated definition satisfies the requirements of the constexpr function, then it will be the constexpr function. For example, from C ++ 17, [class.ctor] / 7:

If this custom default constructor satisfies the requirements of the constexpr constructor (10.1.5), the implicitly defined default constructor is constexpr .

The language around implicit default constructors is described in terms of what the "custom default constructor" will look like. So, "this custom default constructor" means "what the compiler generates."

A similar formulation exists for copy / move constructors.

Conditional wording is somewhat more complicated for assignment operators, but it comes down to the same thing. The type must be literal, and the assignment operators selected for copy / move for each subobject (non-static data element and base class) must be constexpr .

+5
source

All Articles