Variable template in template class - unexpected error (possible bug?)

后端 未结 2 1889
眼角桃花
眼角桃花 2021-01-01 16:37

Having:

struct Value
{
    template
    static constexpr T value{0};
};

(0) ideone

te         


        
相关标签:
2条回答
  • 2021-01-01 17:01

    I've had some headaches before when creating template class header files in c++.

    Make sure you implementation of static constexpr T value{0}; is in the same header file as the declaration.

    0 讨论(0)
  • 2021-01-01 17:05

    This is definitely a gcc and clang bug in their treatment of variable templates as dependent names. I submitted gcc 67248 and clang 24473.

    As a workaround for now, both compilers support the old way of doing variable templates, namely if you added:

    struct Value
    {
        template<class T>
        static constexpr T value = 0;
    
        template <typename T>
        struct variable_template_ish {
            static constexpr T value = Value::value<T>;
        };
    };
    

    then the following compiles:

    template<typename TValue>
    struct Something
    {
        void foo() {
            static_assert(TValue::template variable_template_ish<int>::value == 0, "");
        }
    };
    
    int main() { 
        Something<Value>{}.foo();
    }
    
    0 讨论(0)
提交回复
热议问题