Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constexpr struct member initialisation

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.)

like image 346
Miral Avatar asked Feb 13 '19 00:02

Miral


People also ask

Can a member function be constexpr?

const can only be used with non-static member functions whereas constexpr can be used with member and non-member functions, even with constructors but with condition that argument and return type must be of literal types.

Can struct be constexpr?

Since these structs are pure data, they can be constexpr.

Should constexpr always be static?

The short answer is that not only is static useful, it is pretty well always going to be desired. First, note that static and constexpr are completely independent of each other. static defines the object's lifetime during execution; constexpr specifies that the object should be available during compilation.

When to use #define vs constexpr?

#define directives create macro substitution, while constexpr variables are special type of variables. They literally have nothing in common beside the fact that before constexpr (or even const ) variables were available, macros were sometimes used when currently constexpr variable can be used.


1 Answers

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.

like image 66
n. 1.8e9-where's-my-share m. Avatar answered Oct 04 '22 21:10

n. 1.8e9-where's-my-share m.