c++ - Template Specialization - member functions -
c++ - Template Specialization - member functions -
i'm having problems syntax(assumption) regarding declaration of fellow member function in template specialization.
i have simple class stack
treats every type same except string
s
this have far
//stack.h #ifndef stack_h #define stack_h #include <vector> #include <deque> #include <string> template <typename t> class stack { public: void push(t const&); void pop(); private: std::vector<t> _elems; }; /* template specialization */ template <> class stack <std::string>{ public: void push(std::string const &s); void pop(); private: std::deque<std::string> _elems; }; #include "stack.tpp" #endif // stack_h //stack.tpp #include <stdexcept> template <typename t> void stack<t>::push(t const& t) { _elems.push_back(t); } template <typename t> void stack<t>::pop() { if(!_elems.empty()) _elems.pop_back(); } template<> void stack<std::string>::push(std::string const& s) { _elems.push_back(s); } template <> void stack<std::string>::pop() { if(!_elems.empty()) _elems.pop_back(); }
but error: template-id 'push<>' 'void stack<std::basic_string<char> >::push(const string&)' not match template declaration
i found solutions have fellow member functions declared in .h
file want avoid that.
so have messed up? sense free comment on rest of code(style, readability, efficiency)
the template<>
prefixes not needed function definitions. should need:
void stack<std::string>::push(std::string const& s) { _elems.push_back(s); } void stack<std::string>::pop() { if(!_elems.empty()) _elems.pop_back(); }
c++ templates specialization
Comments
Post a Comment