code_junkie

simple C++ templates suited for STL Containers

Nov 13th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. template <typename container> void mySuperTempalte (const container myCont)
  2. {
  3. //do something here
  4. }
  5.  
  6. template <typename container> void mySuperTempalte (const container<std::string> myCont)
  7. {
  8. //check type of container
  9. //do something here
  10. }
  11.  
  12. template<typename container> void mySuperTempalte( const container &myCont )
  13. {
  14. // Implement STL container code
  15. }
  16.  
  17. template<> void mySuperTempalte( const container<std::string> &myCont )
  18. {
  19. // Implement the string code
  20. }
  21.  
  22. template<typename C> void mySuperTempalte( const std::vector<C> &myCont)
  23. {
  24. // check type of container
  25. // do something here
  26. }
  27.  
  28. template<> void mySuperTempalte<std:string>(const std::string myCont)
  29. {
  30. //check type of container
  31. //do something here
  32. }
  33.  
  34. template<typename C> void mySuperTempalte (std::vector<C> myCont)
  35. {
  36. //check type of container
  37. //do something here
  38. }
  39.  
  40. template<typename C> void mySuperTempalte (std::deque<C> myCont)
  41. {
  42. //check type of container
  43. //do something here
  44. }
  45.  
  46. template <template <typename> class Container>
  47. void mySuperTemplate(Container<std::string> const& cont) {
  48. }
  49.  
  50. //check type of container
  51.  
  52. inline template <typename C> void mySuperTemplate (C const& myCont)
  53. {
  54. mySuperTemplateImpl<C, typename C::value_type>(myCont);
  55. }
  56.  
  57. template<typename C> void mySuperTempalte (std::vector<C> myCont)
  58. {
  59. //check type of container
  60. //do something here
  61. }
  62.  
  63. #include <iostream>
  64. #include <vector>
  65. #include <string>
  66. using namespace std;
  67.  
  68. template <bool B, class T> struct enable_if {};
  69. template <class T> struct enable_if<true, T> { typedef T type; };
  70.  
  71. template <class T, class U> struct is_same { enum { value = false }; };
  72. template <class T> struct is_same<T,T> { enum { value = true }; };
  73.  
  74. namespace detail{
  75. // our special function, not for strings
  76. // use ... to make it the least-prefered overload
  77. template <class Container>
  78. void SpecialFunction_(const Container& c, ...){
  79. cout << "invoked SpecialFunction() defaultn";
  80. }
  81.  
  82. // our special function, first overload:
  83. template <class Container>
  84. // enable only if it is a container of mutable strings
  85. typename enable_if<
  86. is_same<typename Container::value_type, string>::value,
  87. void
  88. >::type
  89. SpecialFunction_(const Container& c, void*){
  90. cout << "invoked SpecialFunction() for stringsn";
  91. }
  92. }
  93.  
  94. // wrapper function
  95. template <class Container>
  96. void SpecialFunction(const Container& c){
  97. detail::SpecialFunction_(c, 0);
  98. }
  99.  
  100. int main(){
  101. vector<int> vi;
  102. cout << "calling with vector<int>n";
  103. SpecialFunction(vi);
  104.  
  105. vector<string> vs;
  106. cout << "ncalling with vector<string>n";
  107. SpecialFunction(vs);
  108. }
  109.  
  110. d:scratch>parameterize.exe calling
  111. with vector<int> invoked
  112. SpecialFunction() default
  113.  
  114. calling with vector<string> invoked
  115. SpecialFunction() for strings
  116.  
  117. d:scratch>
  118.  
  119. enum container_types
  120. {
  121. unknown,
  122. list_container,
  123. vector_container
  124. };
  125.  
  126. template <typename T>
  127. struct detect_container_
  128. {
  129. enum { type = unknown };
  130. };
  131.  
  132. template <typename V>
  133. struct detect_container_< std::vector<V> > // specialization
  134. {
  135. enum { type = vector_container };
  136. };
  137.  
  138. template <typename V>
  139. struct detect_container_< std::list<V> >
  140. {
  141. enum { type = list_container };
  142. };
  143.  
  144. // Helper function to ease usage
  145. template <typename T>
  146. container_types detect_container( T const & )
  147. {
  148. return static_cast<container_types>( detect_container_<T>::type );
  149. }
  150.  
  151. int main()
  152. {
  153. std::vector<int> v;
  154.  
  155. assert( detect_container( v ) == vector_container );
  156. }
Add Comment
Please, Sign In to add comment