Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef STACK_H_
- #define STACK_H_
- template <typename T, template<typename T> class ContainerType>
- class Stack{
- ContainerType<T> container;
- public:
- Stack() : container(ContainerType<T>()){}
- };
- #endif /* STACK_H_ */
- #include "stack.h"
- #include <vector>
- int main(){
- Stack<int, std::vector<int> > stack;
- return 0;
- }
- Stack<int, std::vector<int> > stack;
- expected a class template, got `std::vector<int, std::allocator<int> >' test.cpp
- invalid type in declaration before ';' token test.cpp
- type/value mismatch at argument 2 in template parameter
- list for `template<class T, template<class T> class ContainerType>
- class Stack' test.cpp
-
- template <
- typename T,
- template<typename, typename> class ContainerType,
- typename Alloc = std::allocator<T>
- >
- class Stack{
- ContainerType<T, Alloc> container;
- // ...
- };
- // usage:
- Stack<int, std::vector> s;
- template <typename T, typename ContainerType>
- class Stack{
- ContainerType container;
- // ...
- };
- // usage:
- Stack<int, std::vector<int> > s;
- #include <tr1/type_traits> // C++03 us std::tr1::is_same
- //#include <type_traits> // C++0x, use std::is_same
- template <typename T, typename ContainerType>
- class Stack{
- typedef typename ContainerType::value_type underlying_value_type;
- typedef char ERROR_different_value_type[
- std::tr1::is_same<T, underlying_value_type>::value ? 1 : -1
- ]
- ContainerType container;
- // ...
- };
- #include <tr1/type_traits> // C++03
- //#include <type_traits> // C++0x
- template <typename T, typename ContainerType>
- class Stack{
- typedef typename ContainerType::value_type underlying_value_type;
- static_assert(std::tr1::is_same<T, underlying_value_type>::value,
- "Error: The type of the stack must be the same as the type of the container");
- ContainerType container;
- // ...
- };
- template <typename T, typename ContainerType = std::vector<T>>
- class Stack{
- ContainerType container;
- // ...
- };
- // usage:
- Stack<int> s;
- Stack<int, std::vector > stack;
- Stack<int, vector<int> > stack;
- Stack<int, std::vector<int> > stack;
- using namespace std;
- template <typename Container>
- class Stack
- {
- public:
- typedef typename Container::value_type value_type;
- private:
- Container _container;
- }; // class Stack<Container>
Add Comment
Please, Sign In to add comment