Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3990012
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:26:35+00:00 2026-05-20T06:26:35+00:00

I want to template the >> and << operators in a class, but I

  • 0

I want to template the >> and << operators in a class, but I would also like to specialize them for strings, so I 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 I got some compiler errors:

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

How would I fix these errors?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:26:36+00:00Added an answer on May 20, 2026 at 6:26 am

    You don’t need to specialize the operator template: just write an overload taking a 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 is yucky and should be avoided wherever possible. For more information, consult Herb Sutter’s Why Not Specialize Function Templates?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have a template class that looks something like what I have
I want to create a template class in C#, for example: public class Foo<T>
Consider this simple class: template<class T> class Foo{ public: Foo(T const& val) : _val(val)
Ie. I want to code a class ADT that can be used like this:
I am creating a custom control. I want the template for this control to
i want a specialize template in a pointer-to-member-function case. Is there a way to
I want to have a text template file that would contain variables. the file
I want to use this pure HTML/CSS template for my ASP.NET website: http://sub3.tanguay.de I
I'm using Visual Studio 2008. I have this class: template <bool T1> class Foo
UPDATE: Obviously, you'd want to do this using templates or a base class rather

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.