Constexpr default default constructors

I get a Clang 3.8 compiler error and GCC 5.3 if I want to declare my default constructors defaultas constexpr. According to this stackoverflow question, it should work fine:

struct A
{
    constexpr A() = default;

    int x;
};

However:

Error: defaulted definition of default constructor is not constexpr

Do you have any clue what is really going on?

+4
source share
1 answer

Be that as it may, x remains uninitialized, so the object cannot be created at compile time.

You need to initialize x:

struct A
{
    constexpr A() = default;

    int x = 1;
};
+7
source

All Articles