code_junkie

Why is there &ldquo;class&rdquo; in &ldquo;template <class x>&rdquo;?

Nov 12th, 2011
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. template <class T>
  2. class Something
  3. {
  4. public:
  5. Something(const T &something);
  6. }
  7.  
  8. template<template <class T> class U> // must be "class"
  9. std::string to_string(const U<char>& u)
  10. {
  11. return std::string(u.begin(),u.end());
  12. }
  13.  
  14. template<std::size_t max>
  15. class Foo{...};
  16. ...
  17. Foo<10> f;
  18.  
  19. template<int I>
  20. int getTwice()
  21. {
  22. return I * 2 ;
  23. }
  24.  
  25. template<class T>
  26. std::string getType(const T & t)
  27. {
  28. return typeid(t).name() ;
  29. }
  30.  
  31. void doSomething()
  32. {
  33. std::cout << "25 : " << getTwice<25>() << std::endl ;
  34. std::cout << "5 : " << getTwice<5>() << std::endl ;
  35.  
  36. std::cout << "type(25) : " << getType(25) << std::endl ;
  37. std::cout << "type(25.5) : " << getType(25.5) << std::endl ;
  38. std::cout << "type(abc) : " << getType("abc") << std::endl ;
  39. }
  40.  
  41. 25 : 50
  42. 5 : 10
  43. type(25) : i
  44. type(25.5) : d
  45. type(abc) : A4_c
  46.  
  47. // "I" is the value, and "int" is the type of the value
  48. template <int I>
  49.  
  50. // "T" is a type... And "class" is the "this-is-a-type" keyword
  51. template <class T>
  52.  
  53. template<class T> // T could be any STL container, for example a vector<char>
  54. void printContainerData(const T & t)
  55. {
  56. std::cout << "aVector:" ;
  57.  
  58. for(T::const_iterator it = t.begin(), itEnd = t.end(); it != itEnd; ++it)
  59. {
  60. std::cout << " " << (*it) ;
  61. }
  62.  
  63. std::cout << std::endl ;
  64. }
  65.  
  66. for(class T::const_iterator it = t.begin(), // etc.
  67.  
  68. for(typename T::const_iterator it = t.begin(), // etc.
  69.  
  70. template <typename T>
  71.  
  72. template <class T>
  73.  
  74. template<template <class T> class U> // must be "class"
  75. std::string to_string(const U<char>& u)
  76. {
  77. return std::string(u.begin(),u.end());
  78. }
  79.  
  80. template <class T> class U
  81.  
  82. template <typename T> class U
  83.  
  84. std::string to_string(const U<char>& u)
  85.  
  86. template<typename T>
  87. class U
  88. {
  89. // Etc.
  90. } ;
  91.  
  92. U<char> u ;
  93. // etc.
  94. to_string(u)
  95.  
  96. template<template <class T> class U>
  97. std::string to_string(const U<char>& u)
  98.  
  99. template<template <typename T> class U>
  100. std::string to_string(const U<char>& u)
  101.  
  102. template<typename U>
  103. std::string to_string(const U & u)
  104. {
  105. return std::string(u.begin(),u.end());
  106. }
  107.  
  108. std::list<char> myList ;
  109. // etc.
  110. std::cout << to_string(myList) << std:endl ;
  111.  
  112. template<typename T, int N=4, bool Flag>
  113. class myClass { /*...*/ };
  114.  
  115. template<typename T, const int& Reference, myClass * Pointer&gt
  116. class someClass { /*...*/ };
  117.  
  118. template<typename T, int (T::*MemberFunctionPtr)(int, int)>
  119. class anotherClass { /*...*/ };
Add Comment
Please, Sign In to add comment