【发布时间】:2016-03-02 17:18:39
【问题描述】:
以下内容将使用 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 的成员。
如何修复代码以便在两个编译器上编译?还是完全违法?
【问题讨论】:
-
在clang上也编译失败。
-
是不是语法不合法,而 GCC 只是侥幸脱身?
-
我不确定,但它看起来应该是非法的,否则你可以有
num = Derived::value和value = CRTP<V>::num -
但是在 GCC 上它可以编译,并且在运行我的程序时,所有输出都符合预期,即没有未定义的行为。
-
Derived=A不完整,所以我怀疑这是有效的
标签: c++ templates c++11 constexpr crtp