Reputation: 38214
I want to write two different implementations of class Stack.
(1)
template<typename element_type, typename container_type = std::vector<element_type> >
class Stack{//...
};
(2)
template<typename element_type, size_t container_size>
class Stack{
};
If I define both the implementations in a single file I get compiler error. Is it possible to have both of them in the same file?
//compiler errors:
Stack.hpp:119:46: error: declaration of template ‘template<class element_type, long unsigned int container_size> int containers::Stack()’
Stack.hpp:25:9: error: conflicts with previous declaration ‘template<class element_type, class container_type> class containers::Stack’
Stack.hpp:25:9: error: previous non-function declaration ‘template<class element_type, class container_type> class containers::Stack’
Stack.hpp:119:46: error: conflicts with function declaration ‘template<class element_type, long unsigned int container_size> int containers::Stack()’
Upvotes: 0
Views: 165
Reputation: 5135
You have to write something like this:
template<typename element_type, typename container_type = std::vector<element_type> >
class Stack {
// This is *not* the specialization
};
template<typename element_type>
class Stack<element_type, std::vector<element_type> > {
// Specialization goes here.
};
Edit: You can't change the type of the template parameter.
Upvotes: 2
Reputation: 1081
Nope, you can't. You can't have two versions of a templated class with different template parameters. I recommend calling the two kinds of Stack
differently or putting them in different namespaces.
Upvotes: 3
Reputation: 64308
You have to give them two different names if they take different kinds of parameters.
Upvotes: 1