I recently came across this very weird problem: if we have a template class like the one below:
template<typename T>
class A
{
public:
void f(void);
void g(void);
};
and I want my f() function to be available for any type 'T', but I want g() to only be available when the template is instanced with 'int', C++ allows me to make the following function definitions:
template<typename T>
inline void A<T>::f(void)
{
//code here
}
template<>
inline void A<int>::g(void)
{
//code here
}
However, if we add another parameter to the template, which makes our class look like this:
template<typename T, typename U>
class A
{
public:
void f(void);
void g(void);
};
and I want similar behaviors as above for f() and g(), meaning that I'd want f() available for any 'T' and 'U' types, but g() only available when 'U' is an 'int' ('T' remains generic), I receive a compilation error for g(), which apparently cannot be partially specialized as before:
template<typename T, typename U>
inline void A<T, U>::f(void)
{
//code here
}
template<typename T>
inline void A<T, int>::g(void)
{
//code here
}
Why is this happening? If in our first case, when we had just 1 parameter, I can "partially specialize" the template and not include the type which is explicit (non-generic anymore), why doesn't this behaviour work for more than 1 generic parameter? I've been busting my head with this for 6 days now.
Thank you so much!!!