C++1y/C++14: Variable Template Specialization?

后端 未结 4 2030
[愿得一人]
[愿得一人] 2020-12-13 18:02

According to C++1y/C++14 N3690, does the type of a variable template specialization have to be the same as the type of the primary template?

template

        
4条回答
  •  星月不相逢
    2020-12-13 18:56

    It's a bit risky to extrapolate that Clang is expressing the feature intended for standardization. (NB: I didn't refer to anything for this answer.)

    Of course the fallout from allowing a change of type is that you can't specialize the template after it's been referenced in any way, whereas for all other kinds of templates the cutoff time is at ODR-use. Unless they're planning something wacky, this looks like a Clang bug.

    You can always use a type template to declare the type of the variable template.

    template< typename t >
    struct x_type { typedef … type; };
    
    template< typename t >
    typename x_type< t >::type x = …;
    

    Now x_type may be specialized. This just guards against the possibility that Clang is currently buggy. It doesn't allow you to refer to an object of indeterminate type. C++ just doesn't support that. Referring to the object ODR-uses the class template specialization.

提交回复
热议问题