constexpr struct member initialisation

一曲冷凌霜 提交于 2020-06-12 04:33:26

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/54660899/constexpr-struct-member-initialisation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!