Question

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. Is there a way to set the x_trait for all templated tt_types? 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; };
Was it helpful?

Solution

You can partially specialise the x_trait template for all specialisations of the tt_type template:

template<typename T> 
struct x_trait<tt_type<T>> { static const bool has_x = false; };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top