plasmacel
plasmacel

Reputation: 8530

constexpr defaulted default constructors

I get compiler error by Clang 3.8 and GCC 5.3 if I want to declare my default-ed default constructors as constexpr. According to this stackoverflow question it just should work fine:

struct A
{
    constexpr A() = default;

    int x;
};

however:

Error: defaulted definition of default constructor is not constexpr

Have you got any clue what is actually going on?

Upvotes: 13

Views: 8893

Answers (1)

Jorn Vernee
Jorn Vernee

Reputation: 33845

As it stands, x remains uninitialized, so the object can not be constructed at compile time.

You need to initialize x:

struct A
{
    constexpr A() = default;

    int x = 1;
};

Upvotes: 20

Related Questions