c++ - Template << and >> operators specialization -


i want template >> , << operators in class, specialize them strings, did this;

    class sql_command { public:     sql_command() { }      explicit sql_command(std::string& cmd)         : m_raw(cmd) {     }      inline sql_command& operator<<(const char * arg)     {         m_raw += arg;         return *this;     }      inline sql_command& operator<<(const std::string& arg)     {         m_raw += arg;         return *this;     }      template<typename t>     inline sql_command& operator>>(const t arg)     {         m_raw += boost::lexical_cast<std::string>(arg);         return *this;     }      inline std::string const& command() const {         return m_raw;     }  private:     std::string m_raw; };  template<> inline sql_command& operator>> <std::string> (const std::string& arg) {     m_raw += "'";     m_raw += arg;     m_raw += "'";     return *this; }  template<> inline sql_command& operator>> <char*>(const char * arg) {     m_raw += "'";     m_raw += arg;     m_raw += "'";     return *this; } 

but got compiler errors:

1>.\main.cpp(83) : error c2912: explicit specialization; 'sql_command &operator >><std::string>(const std::string &)' not specialization of function template 1>.\main.cpp(83) : error c2805: binary 'operator >>' has few parameters 

how fix these errors?

you don't need specialize operator template: write overload taking std::string:

class sql_command {     /* ... */      template<typename t>     sql_command& operator>>(const t& arg) {          /* general purpose implementation */      }      sql_command& operator>>(const std::string& arg) {          /* special std::string implementation */      } }; 

function template specialization yucky , should avoided wherever possible. more information, consult herb sutter's why not specialize function templates?


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -