In the code below, when the signified code is commented out, code works as expected – Type matches the (first) template argument.
However, if I uncomment it, Type becomes char instead of std::string. This is the same with both MSVC and GCC. So it works when I have specialization of StripTag for a tag template with one and two arguments, but when I specialize it in exactly the same way for three arguments, I get this strange behavior.
Anyone has any ideas?
Code follows:
#include <typeinfo>
#include <stdio.h>
#include <string>
template <typename T>
struct StripTag
{typedef T Type;};
template<typename T, template<typename T> class Tag >
struct StripTag<Tag<T> >
{ typedef typename StripTag<T>::Type Type; };
template<typename T, typename X, template<typename T, typename X> class Tag >
struct StripTag<Tag<T,X> >
{ typedef typename StripTag<T>::Type Type; };
/*
//UNCOMMENT THIS AND RECOMPILE
template<typename T, typename X, typename Y, template<typename T, typename X, typename Y> class Tag >
struct StripTag<Tag<T,X,Y> >
{ typedef typename StripTag<T>::Type Type; };
*/
template <class C>
struct Test
{
typedef C Type;
};
template <typename A, typename B>
struct Blah{};
int main()
{
printf("typeid of StripTag=\t%s\n", typeid(StripTag<std::string>::Type).name());
printf("typeid of StripTag2=\t%s\n", typeid(StripTag<Blah<std::string, bool> >::Type).name());
printf("typeid of Test=\t\t%s\n", typeid(Test<std::string>::Type).name());
printf("typeid of std::string=\t%s\n", typeid(std::string).name());
}
This is because
std::stringis really just a typedef forThis, as you can see, is a class template with three type parameters.
When you have a
StripTagspecialization that takes a three-parameter class template as one of its parameters, that specialization is a better match forstd::stringthan the primary class template (because it is more specific).