I have the following code that uses specialization to declare a typedef name type
depending on the value of the non-template parameter value
. As you can see below, the program works as expected but I would like to avoid code duplication that is shown in the comments of the program. In particular, I want that I don't have to repeat the 3 member function declarations or any other code except the typedef.
template<bool value> struct S
{
typedef int type;
//a lot of code(declaraions, definitions etc) here for example
type size();
void func();
int bar(int);
};
//specialization
template<> struct S<false>
{
typedef unsigned type;
//I WANT TO AVOID THIS REPETATION
type size();
void func();
type bar(type);
};
How can I achieve this for the code shown above. Note that the above is a minimal reproducible example that I created which has only one specialization as the template parameter is of type bool
(which can have only true
or false
) but in practical example, the template parameter can be of some other type in which case(as you could imagine) it would become very tedious to repeat the code with the number of specialization.