在下面的代码中,我尝试使用CRTP来使用Parent类中Child类的静态成员"value".当编译使用g ++ 5.2.1与"-pedantic"标志的代码,我能够编译预期,并且在执行双方c.print_value();并Child<int,4>::print_value();打印出4.
#include <iostream>
template <typename DE>
struct Parent
{
static const int value = DE::value;
static void print_value ()
{
std::cout << "Value : " << value << '\n';
}
};
template <typename T, int N>
struct Child : Parent< Child<T,N> >
{
static const int value = N;
};
int
main ()
{
Child<int,4> c;
c.print_value();
Child<int,4>::print_value();
}
Run Code Online (Sandbox Code Playgroud)
但是,当使用clang ++ 3.7编译相同的代码时,我遇到了编译失败.
crtp_clang_error.cpp:9:32: error: no member named 'value' in 'Child<int, 4>'
static const int …Run Code Online (Sandbox Code Playgroud)