【发布时间】:2016-06-21 15:35:06
【问题描述】:
考虑下面我的简单示例:
#include <iostream>
template <typename T>
class Base
{
public:
static constexpr int y = T::x;
};
class Derived : public Base<Derived>
{
public:
static constexpr int x = 5;
};
int main()
{
std::cout << Derived::y << std::endl;
}
在 g++ 中,这可以正常编译并按预期打印 5。但是,在 Clang 中,它无法编译并出现错误 no member named 'x' in 'Derived'。据我所知,这是正确的代码。我正在做的事情有问题吗,如果没有,有没有办法在 Clang 中进行这项工作?
【问题讨论】:
-
如果你在
Base类之外初始化y会起作用吗? -
@Ben 你不能在课堂外初始化
constexpr static int。 -
另外,请参阅这个问题 - stackoverflow.com/questions/37816186/… - 他们说您看到的错误符合标准,如果您真的想这样做,您需要通过函数获取常量。
标签: c++ templates c++11 clang crtp