我想让一个类C拥有一个类型为C的静态constexpr成员。在C++11中是否可能实现?
尝试1:
struct Foo {
constexpr Foo() {}
static constexpr Foo f = Foo();
};
constexpr Foo Foo::f;
g++ 4.7.0 报错:'invalid use of incomplete type',指的是对 Foo()
的调用。
尝试2:
struct Foo {
constexpr Foo() {}
static constexpr Foo f;
};
constexpr Foo Foo::f = Foo();
现在问题是类定义中缺少constexpr
成员f
的初始化器。
尝试3:
struct Foo {
constexpr Foo() {}
static const Foo f;
};
constexpr Foo Foo::f = Foo();
现在g++抱怨Foo::f
的重新声明与constexpr
不同。
static const
与不完整类型的使用方式不一致。我想现在只能暂时使用static const
了! - Matt Clarkson