[英]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. 因此,当编译
two
和three
时,类尚未完全定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.