CRTP和静态成员初始化

时间:2020-07-12 18:29:18

标签: c++

当我编译以下内容时

template <typename Derived> struct Bar 
{
     static inline constexpr int x = Derived::value;
};

struct Foo : Bar<Foo>
{
    static inline constexpr int value = 101;
};

使用VS 2019 16.6时出现编译错误

Source.cpp(5,47): error C2039: 'value': is not a member of 'Foo'
Source.cpp(8): message : see declaration of 'Foo'
Source.cpp(9): message : see reference to class template instantiation 'Bar<Foo>' being compiled
Source.cpp(5,1): error C2065: 'value': undeclared identifier
Source.cpp(5,1): error C2131: expression did not evaluate to a constant
Source.cpp(5,1): message : a non-constant (sub-)expression was encountered

那么从CRTP派生类初始化静态变量为true非法还是编译器错误? 我当然发现错误“值”:不是“ Foo”的成员,因为值显然是成员。

0 个答案:

没有答案