8

This code compiles:

struct Info
{
    constexpr Info(bool val) : counted(false), value(unsigned(val)) {}
    constexpr Info(unsigned val) : counted(true), value(val) {}

    bool counted;
    unsigned value;
};

constexpr const auto data = std::array{
    Info{true}, Info{42u}
};

struct Foo
{
    constexpr static inline const auto data = std::array{
        Info{true}, Info{42u}
    };
};

This code does not:

struct Foo
{
    struct Info
    {
        constexpr Info(bool val) : counted(false), value(unsigned(val)) {}
        constexpr Info(unsigned val) : counted(true), value(val) {}

        bool counted;
        unsigned value;
    };

    constexpr static inline const auto data = std::array{
        Info{true}, Info{42u}
    };
};

The reported error (in MSVC, gcc, and clang) suggests that they think the Info constructor is not defined or is not constexpr, eg. from clang:

prog.cc:21:5: note: undefined constructor 'Info' cannot be used in a constant expression
    Info{true}, Info{42u}
    ^

Why?

(Possibly related to this question, but Info should be complete at the point of use; only Foo is still incomplete.)

xskxzr
  • 12,442
  • 12
  • 37
  • 77
Miral
  • 12,637
  • 4
  • 53
  • 93
  • 1
    Possible duplicate of [Static templated constexpr nested class member](https://stackoverflow.com/questions/39381076/static-templated-constexpr-nested-class-member) – Kunal Puri Feb 13 '19 at 01:09
  • See this: https://stackoverflow.com/questions/45841822/static-constexpr-member-of-an-inner-class – Kunal Puri Feb 13 '19 at 01:10
  • @KunalPuri I already linked to the first answer and explained why it is inadequate in this case. The second question has a comment that partly addresses it, but it isn't an "answer" and it doesn't actually make sense for that restriction to exist. – Miral Feb 13 '19 at 05:08

1 Answers1

6

The error message of gcc-8 is arguably more clear:

   constexpr Foo::Info::Info(bool)’ called in a constant expression before 
   its definition is complete

It seems the error is produced according to [expr.const] §2:

An expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (4.6), would evaluate one of the following expressions:

...

(2.3) — an invocation of an undefined constexpr function or an undefined constexpr constructor;

How come it is undefined, when the call is clearly after the definition?

The thing is, member function definitions are delayed until the closing brace of the outermost enclosing class (because they can see members of enclosing classes). Consider this class definition:

constexpr int one = 1;

struct Foo
{
    struct Bar
    {
        static constexpr int get_one() { return one; }
    };

    static constexpr int two = Bar::get_one() + 1;
    static constexpr int one = 42;
};

Assuming this should work, how could an implementation process this definition?

one inside Bar::get_one refers to Foo::one, not ::one, so it must be processed after that member is seen. It is used in the definition of two, which is constexpr, so it must be processed before the initialiser of that member. So for this to work, the overall order must be one, then get_one, then two.

But C++ implementation don't work this way. They don't do any complicated dependency analysis. They process declarations and definitions in order of being seen, with some exceptions listed in [class.mem] §2.

I cannot seem to find an explicit mention in the standard that a constexpr member function is considered undefined until the oitermost enclosing class is complete, but this is the only logical possibility. It cannot work any other way.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243