Guest User

Untitled

a guest
Jul 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #ifndef STACK_H_
  2. #define STACK_H_
  3.  
  4. template <typename T, template<typename T> class ContainerType>
  5. class Stack{
  6. ContainerType<T> container;
  7. public:
  8. Stack() : container(ContainerType<T>()){}
  9.  
  10. };
  11.  
  12. #endif /* STACK_H_ */
  13.  
  14. #include "stack.h"
  15. #include <vector>
  16.  
  17. int main(){
  18. Stack<int, std::vector<int> > stack;
  19. return 0;
  20. }
  21.  
  22. Stack<int, std::vector<int> > stack;
  23.  
  24. expected a class template, got `std::vector<int, std::allocator<int> >' test.cpp
  25.  
  26. invalid type in declaration before ';' token test.cpp
  27.  
  28. type/value mismatch at argument 2 in template parameter
  29. list for `template<class T, template<class T> class ContainerType>
  30. class Stack' test.cpp
  31.  
  32.  
  33. template <
  34. typename T,
  35. template<typename, typename> class ContainerType,
  36. typename Alloc = std::allocator<T>
  37. >
  38. class Stack{
  39. ContainerType<T, Alloc> container;
  40. // ...
  41. };
  42.  
  43. // usage:
  44. Stack<int, std::vector> s;
  45.  
  46. template <typename T, typename ContainerType>
  47. class Stack{
  48. ContainerType container;
  49. // ...
  50. };
  51.  
  52. // usage:
  53. Stack<int, std::vector<int> > s;
  54.  
  55. #include <tr1/type_traits> // C++03 us std::tr1::is_same
  56. //#include <type_traits> // C++0x, use std::is_same
  57.  
  58. template <typename T, typename ContainerType>
  59. class Stack{
  60. typedef typename ContainerType::value_type underlying_value_type;
  61. typedef char ERROR_different_value_type[
  62. std::tr1::is_same<T, underlying_value_type>::value ? 1 : -1
  63. ]
  64. ContainerType container;
  65. // ...
  66. };
  67.  
  68. #include <tr1/type_traits> // C++03
  69. //#include <type_traits> // C++0x
  70.  
  71. template <typename T, typename ContainerType>
  72. class Stack{
  73. typedef typename ContainerType::value_type underlying_value_type;
  74. static_assert(std::tr1::is_same<T, underlying_value_type>::value,
  75. "Error: The type of the stack must be the same as the type of the container");
  76. ContainerType container;
  77. // ...
  78. };
  79.  
  80. template <typename T, typename ContainerType = std::vector<T>>
  81. class Stack{
  82. ContainerType container;
  83. // ...
  84. };
  85.  
  86. // usage:
  87. Stack<int> s;
  88.  
  89. Stack<int, std::vector > stack;
  90.  
  91. Stack<int, vector<int> > stack;
  92.  
  93. Stack<int, std::vector<int> > stack;
  94.  
  95. using namespace std;
  96.  
  97. template <typename Container>
  98. class Stack
  99. {
  100. public:
  101. typedef typename Container::value_type value_type;
  102.  
  103. private:
  104. Container _container;
  105. }; // class Stack<Container>
Add Comment
Please, Sign In to add comment