C++ 铿锵++-3.7 CRTP编译错误“;“无指定成员”;在父';s模板参数

C++ 铿锵++-3.7 CRTP编译错误“;“无指定成员”;在父';s模板参数,c++,templates,g++,clang++,crtp,C++,Templates,G++,Clang++,Crtp,在下面的代码中,我试图使用CRTP来使用父类中的子类的静态成员“value”。当使用带有“-pedantic”标志的g++5.2.1编译代码时,我能够按照预期进行编译,并且在执行时c.print_value()和Child::print_value()打印输出4 #include <iostream> template <typename DE> struct Parent { static const int value = DE::value; st

在下面的代码中,我试图使用CRTP来使用父类中的子类的静态成员“value”。当使用带有“-pedantic”标志的g++5.2.1编译代码时,我能够按照预期进行编译,并且在执行时
c.print_value()
Child::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();
}
#包括
模板
结构父级
{
静态常量int value=DE::value;
静态无效打印值()
{
标准::cout
^
crtp_clang_error.cpp:38:16:注意:在这里请求的模板类“Child”的实例化中
儿童c;
^
crtp_-clang_错误。cpp:40:3:错误:在“Child”中没有名为“print_-value”的成员;您的意思是“Parent::print_-value”吗?
Child::print_value();
^~~~~~~~~~~~~~~~~~~~~~~~~
父项::打印值
crtp_-clang_错误。cpp:11:15:注意:此处声明了“父::打印值”
静态无效打印值()

我不确定这是一个铿锵++错误还是一个GCC黑客。我非常希望您能提供一些见解。

您继承自
,此时
仍然是一个不完整的类型。通常,您不能使用typedefs,或者CRTP基类主体中派生类的成员,例如,请参见,因此我不希望它使用使用静态成员…但我不确定:)@melak47我同意,这正是错误(不完整类型)如果我在icc 14.0.2中尝试这段代码,我会遇到这种情况。因此我希望icc和clang++的行为是有效的。我想知道为什么g++5.2.1没有抱怨。嗯,
Parent::value
仅在运行时使用。如果在编译时添加了需要该值的内容,那么您也会从GCC得到不完整的类型错误:。可能是不同的GCC和Clang如何处理静态常量变量的初始化?@melak47捕捉得很好。这现在更有意义了。
crtp_clang_error.cpp:9:32: error: no member named 'value' in 'Child<int, 4>'
static const int value = DE::value;
                       ~~~~^
crtp_clang_error.cpp:27:16: note: in instantiation of template class 'Parent<Child<int, 4> >' requested here
struct Child : Parent< Child<T,N> >
           ^
crtp_clang_error.cpp:38:16: note: in instantiation of template class 'Child<int, 4>' requested here
  Child<int,4> c;
           ^
crtp_clang_error.cpp:40:3: error: no member named 'print_value' in 'Child<int, 4>'; did you mean 'Parent<Child<int, 4> >::print_value'?
  Child<int,4>::print_value();
  ^~~~~~~~~~~~~~~~~~~~~~~~~
  Parent<Child<int, 4> >::print_value
crtp_clang_error.cpp:11:15: note: 'Parent<Child<int, 4> >::print_value' declared here
  static void print_value ()