0
votes

The goal of writing this code was to get a better understanding of partial template specialization. I'm trying to partial specialize the class Vector with three different bools.

I have an enum(for my bool) defined as:

enum MY_BOOL
{
   YES,
   NO,
   MAYBE
};

For my primary template class I have

template<class A,MY_BOOL,class B>
class Vector{};

And the partial specialization class I have is

template<MY_BOOL b>
class Vector<A,YES,B>{};

The compiler is complaining that A and B are undeclared identifiers and that the partial specialized Vector has too few arguments. Doesn't complain about 'YES' This confuses me because A and B were already defined in the primary template class. I shouldn't need to put them back in the parameter list of the partial specialized class because the point of that parameter list is to only have the variables that I want specialized.

2
Are A and B actual types in your partial specialization? Please add a minimal reproducible example.NathanOliver
@NathanOliver they're not like int or char but there purpose was to be passed in a different template struct. The keyword class should of dealt with this?BrogrammerDude
You can't do a specialization like that. A and B only apply to template<class A,MY_BOOL,class B> class Vector{};. In template<MY_BOOL b> class Vector<A,YES,B>{}; they do not exist.NathanOliver
@NathanOliver so in other words I have to include A and B in my template parameter list for the partial specialized template class?BrogrammerDude
Yes, but then it wouldn't be a partial specialization. Why are you trying to achieve here?NathanOliver

2 Answers

1
votes

In

template<MY_BOOL b>
class Vector<A,YES,B>{};

Since A and B aren't specified, you get a compiler error. It is not going to use the A and B from the primary template, it will only use the types/value defined in the specialization.

Since you want a specialization for each of the enum values you can do that like

template<class A,MY_BOOL,class B>
class Vector {};

template<class A, class B>
class Vector<A, YES, B>{ /* YES stuff */ };

template<class A, class B>
class Vector<A, NO, B>{ /* NO stuff */ };

template<class A, class B>
class Vector<A, MAYBE, B>{ /* MAYBE stuff */ };

And now you have a specialization for each of the enumerations.

0
votes

A partial specialization for YES would look like:

template<class A, class B>
class Vector<A, YES, B>
{ ... };

The meaning of partial specialization is that you provide different template arguments than the base template and fill in the missing template parameters of the base template yourself.