简体   繁体   English

与类定义相同类型的静态constexpr成员(其他详细信息)

[英]Static constexpr members of same type as class defined (additional details)

While writing my initial question, if this is even possible, I stumbled about the question static constexpr member of same type as class being defined , which quite clearly answered that my clean solution is not possible with C++11. 在编写我的初始问题时,如果这是可能的,我偶然发现了与定义类相同类型的静态constexpr成员的问题,这很清楚地回答了我的干净解决方案无法用C ++ 11实现。

But then I came up with this code which is quite close to the original poster and I want to achieve: 但后来我提出了这个与原始海报非常接近的代码,我希望实现:

class MyEnum
{
public:
    constexpr MyEnum() : m_null(true), m_value(0) { }
    constexpr MyEnum(const unsigned int v) : m_null(false), m_value(v) { }

    constexpr operator unsigned int() const { return m_value; }

    static constexpr const MyEnum one() { return MyEnum(1); }

private:
    bool m_null;
    unsigned int m_value;
};

So I'm rephrasing my question: Why does the solution for one compile and can be used as you would expect it but the following solutions give errors about using an incomplete class? 所以我在重述我的问题:为什么one编译的解决方案可以像你期望的那样使用,但是下面的解决方案会给出使用不完整类的错误?

class MyEnum
{
public:
    // snip...

    static constexpr const MyEnum two = MyEnum(2);
    static constexpr const MyEnum three = 3;

    // snip...
}

As @dyp mentioned the solution for one compiles because function definitions are compiled after the class body. 由于@dyp提到了one编译的解决方案,因为函数定义是在类主体之后编译的。 So it is like one has been declared like this 所以就像one人被宣布为这样

class MyEnum
{
public:
    static constexpr const MyEnum one();
    //... Definition here
};  //Class is fully defined after here

inline static constexpr const MyEnum MyEnum::one() { return MyEnum(1); }
                      //Fine here because class is complete ^^^^

On the other hand, definitions in the class body are compiled as they are placed in the class body. 另一方面,类主体中的定义在它们放置在类主体中时被编译。 So when two and three are being compiled the class is not fully defined yet. 因此,当编译twothree时,类尚未完全定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与定义的类相同类型的静态constexpr成员 - static constexpr member of same type as class being defined 定义一个 static 同类型模板 class 的 constexpr 成员 - Define a static constexpr member of same type of a template class 如何初始化属于类的成员并且与它们所在的类具有相同类型的静态对象? - How are static objects, that are members of a class and that are of the same type as the class they are in, initialized? 如何根据 class 实例化基础初始化 constexpr static class 成员? - How to initialize constexpr static class members per class instantiation basis? 使用正在定义的 class 的 static class 成员 - Using static class members of the class being defined 如何在C ++中将用户定义的文字放在同一类型的constexpr类中? - How to place user-defined literal inside constexpr class of same type in C++? 解决`constexpr` static 数据成员的限制,其类型与封闭 class 相同 - Working around limitation of `constexpr` static data member with same type as enclosing class 定义constexpr静态数据成员 - Defining constexpr static data members constexpr班级成员的利弊 - Pros and cons of constexpr class members 将全局constexpr类作为静态constexpr移动到类中 - Move global constexpr class into class as static constexpr
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM