Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 1.15 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Strange behaviour of the following template code
  2. #include <typeinfo>
  3. #include <stdio.h>
  4. #include <string>
  5.  
  6. template <typename T>
  7. struct StripTag
  8. {typedef T Type;};
  9.  
  10. template<typename T, template<typename T> class Tag >
  11. struct StripTag<Tag<T> >
  12. { typedef typename StripTag<T>::Type Type; };
  13.  
  14. template<typename T, typename X, template<typename T, typename X> class Tag >
  15. struct StripTag<Tag<T,X> >
  16. { typedef typename StripTag<T>::Type Type; };
  17.  
  18. /*
  19. //UNCOMMENT THIS AND RECOMPILE
  20. template<typename T, typename X, typename Y, template<typename T, typename X, typename Y> class Tag >
  21. struct StripTag<Tag<T,X,Y> >
  22. { typedef typename StripTag<T>::Type Type; };
  23. */
  24.  
  25. template <class C>
  26. struct Test
  27. {
  28. typedef C Type;
  29. };
  30.  
  31. template <typename A, typename B>
  32. struct Blah{};
  33.  
  34. int main()
  35. {
  36.  
  37.     printf("typeid of StripTag=t%sn", typeid(StripTag<std::string>::Type).name());
  38.     printf("typeid of StripTag2=t%sn", typeid(StripTag<Blah<std::string, bool> >::Type).name());
  39.     printf("typeid of Test=tt%sn", typeid(Test<std::string>::Type).name());
  40.     printf("typeid of std::string=t%sn", typeid(std::string).name());
  41. }
  42.        
  43. std::basic_string<char, std::char_traits<char>, std::allocator<char>>