Reputation: 60451
For a class of type T, the following members can be generated by the compiler, depending on the class:
T::T()
T::T(const T&)
T::T(T&&)
T& T::operator=(const T&)
T& T::operator=(T&&)
In C++14, and in C++17, what are the rules that lead to the generation of constexpr
versions of these functions by the compiler?
Upvotes: 5
Views: 870
Reputation: 474186
The rule is simple: if the generated definition satisfies the requirements of a constexpr
function, then it will be a constexpr
function. For example, from C++17, [class.ctor]/7:
If that user-written default constructor would satisfy the requirements of a constexpr constructor (10.1.5), the implicitly-defined default constructor is
constexpr
.
The wording around implicit default constructors is describes in terms of what a "user-written default constructor" would look like. So "that user-written default constructor" means "what the compiler generates".
Similar wording exists for the copy/move constructors.
The wording is slightly more complex for the assignment operators, but it boils down to the same thing. The type must be a literal type and the assignment operators selected to do the copy/move for each subobject (non-static data member and base class) must be constexpr
.
Upvotes: 5