Clang++-3.7 CRTP 编译错误“父级模板参数中没有命名成员”

2023-12-19

在下面的代码中,我尝试使用 CRTP 来使用父类中子类的静态成员“值”。当使用带有“-pedantic”标志的 g++ 5.2.1 编译代码时,我能够按预期编译,并且在执行时c.print_value(); and 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();
}

但是,当使用 clang++3.7 编译相同的代码时,我遇到编译失败。

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 ()

我不确定这是 Clang++ bug 还是 GCC hack。非常感谢一些见解。


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Clang++-3.7 CRTP 编译错误“父级模板参数中没有命名成员” 的相关文章