Constexpr conditions for the constructor

This site indicates that:

"The constexpr function shall satisfy the following requirements:

[...]

there is at least one set of argument values, so a function call can be evaluated by a subexpression of the main constant expression ( for constructors, using a constant initializer is enough ) (since C ++ 14). No diagnostics are required to violate this bullet. "

What is the meaning of the expression bolded?

+7
c ++ oop c ++ 14 constexpr
source share
1 answer

Looking for a related defect report

struct X { std::unique_ptr<int> p; constexpr X() { } }; 

Prior to C ++ 14, this will be poorly formed due to [dcl.constexpr]

For the constexpr constructor, if there is no argument value, so after replacing the call function, each constructor call and the full expression in the mem initializers will be a constant expression (including conversions), the program is poorly formed; no diagnostics required.

Which means that there is some argument (in this case, only an empty set) that can create a constant expression for calling X::X , as in

 constexpr X x; // must be valid before C++14 

Since std::unique_ptr not a literal type, it has a nontrivial destructor, this is not possible. However, the constexpr report suggested that constexpr constructors constexpr still be well formed in such cases due to this type of use

 X x; // not constexpr, but initialization should be constant 

Therefore, the record

For a constexpr function or constexpr constructor, which is neither a default nor a template, if there are no argument values, so a function or constructor call can be evaluated by subexpressing the main constant expression or for a constructor, a constant initializer for some object , the program is poorly formed, not diagnostics required.

Translated, this means: the constexpr constructor constexpr well formed as long as it is a constexpr function, and its member initializations are also constexpr functions, even if the type itself can never be constexpr .

+4
source share

All Articles