简体   繁体   English

模板化类型的模板专业化

[英]Template specialization for templated type

I have a struct that contains a trait about a type: 我有一个结构,其中包含有关类型的特征:

template<typename T> struct x_trait { static const bool has_x = true; };

That is correct for all types but for a certain template type. 这对于除特定模板类型之外的所有类型都是正确的。 For that certain template type I want to change the trait: 对于某些模板类型,我想更改特征:

template<> struct x_trait<tt_type<int>> { static const bool has_x = false; };

So far, so good. 到现在为止还挺好。 But the tt_type itself takes different template parameters. 但是tt_type本身采用了不同的模板参数。 Is there a way to set the x_trait for all templated tt_type s? 有没有一种方法来设置x_trait所有模板tt_type S' Right now my only way out is to list all types: 现在,我唯一的出路是列出所有类型:

template<> struct x_trait<tt_type<char>> { static const bool has_x = false; };
template<> struct x_trait<tt_type<short>> { static const bool has_x = false; };
template<> struct x_trait<tt_type<int>> { static const bool has_x = false; };
template<> struct x_trait<tt_type<long>> { static const bool has_x = false; };

You can partially specialise the x_trait template for all specialisations of the tt_type template: 您可以为x_trait模板的所有x_trait化部分地专门化tt_type模板:

template<typename T> 
struct x_trait<tt_type<T>> { static const bool has_x = false; };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM