Constexpr Default Functions

Viewed 76

Are default functions that can be constexpr implicitly constexpr?

class c
{
public:
    constexpr /*<- necessary?*/ c() = default;
    constexpr /*<- necessary?*/ c(c const&) = default;
    constexpr /*<- necessary?*/ c(c &&) = default;
    constexpr /*<- necessary?*/ c& operator =(c const&) = default;
    constexpr /*<- necessary?*/ c& operator =(c &&) = default;
    ~c() = default;

private:
    int x = 0;
};

What about other qualifiers, like noexcept?

1 Answers

Yes, defaulted special members are constexpr if they satisfy requirements for a constexpr member. In particular, see 11.4.4.1/4 for default constructor:

A default constructor that is defaulted and not defined as deleted is implicitly defined... The implicitly-defined default constructor performs the set of initializations of the class that would be performed by a user-written default constructor for that class with no ctor-initializer and an empty compound-statement. ... If that user-written default constructor would satisfy the requirements of a constexpr constructor ([dcl.constexpr]), the implicitly-defined default constructor is constexpr. Before the defaulted default constructor for a class is implicitly defined, all the non-user-provided default constructors for its base classes and its non-static data members are implicitly defined. ...

Similar wording exists for other special members.

Related