以下内容将使用 GCC 5.2 进行编译,但不能使用 Visual Studio 2015 进行编译。
template <typename Derived>
struct CRTP {
static constexpr int num = Derived::value + 1;
};
struct A : CRTP<A> {
static constexpr int value = 5;
};
它抱怨说A
没有名为的成员value
。
如何修复代码以便它可以在两个编译器上编译?或者说这完全是非法的吗?
尝试将其设为 constexpr 函数。您现在的设置方式尝试访问不完整的类型。
由于模板化成员函数仅在首次使用时才会初始化,因此请键入A
将由这一点完全定义。
#include <iostream>
template <typename Derived>
struct CRTP {
static constexpr int num() { return Derived::value + 1; }
};
struct A : CRTP<A> {
static constexpr int value = 5;
};
int main()
{
std::cout << A::num();
return 0;
}
现场观看here
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)