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

  • Home
  • SEARCH
  • 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 222373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:07:16+00:00 2026-05-11T19:07:16+00:00

I’ve created a small utility function for string conversion so that I don’t have

  • 0

I’ve created a small utility function for string conversion so that I don’t have to go around creating ostringstream objects all over the place

template<typename T>
inline string ToString(const T& x)
{
    std::ostringstream o;
    if (!(o << x))
        throw BadConversion(string("ToString(") + typeid(x).name() + ")");
    return o.str();
}

I want to provide some specialisations of this method for instances where there is no default overloaded << operator for stringstream (i.e. std::pair, std::set, my own classes) and I’ve run into difficulties with the templating. I will illustrate with the std::pair example, if I want to be able to

string str = ToString(make_pair(3, 4));

the only way I can think of to do it is to define the explicit specialisation for int

template<>
inline string ToString(const pair<int,int>& x)
{
    std::ostringstream o;
    if (!(o << "[" << x.first << "," << x.second << "]"))
        throw BadConversion(string("ToString(pair<int,int>)"));
    return o.str();
}

is there a way I can define this for the generic case?

template<>
inline string ToString(const pair<T1,T2>& x)
{
    std::ostringstream o;
    if (!(o << "[" << x.first << "," << x.second << "]"))
        throw BadConversion(string("ToString(pair<T1,T2>)"));
    return o.str();
}   
  • 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-11T19:07:16+00:00Added an answer on May 11, 2026 at 7:07 pm

    Don’t specialize the template, but overload it. The compiler will figure out what function template to take by ordering them according to their specialization of their function parameter types (this is called partial ordering).

    template<typename T1, typename T2>
    inline string ToString(const std::pair<T1, T2>& x) {
        std::ostringstream o;
        if (!(o << "[" << x.first << "," << x.second << "]"))
            throw BadConversion(string("ToString(pair<T1,T2>)"));
        return o.str();
    }
    

    In general, partial ordering will result in exactly what you expect. In more detail, consider having these two functions

    template<typename T> void f(T);
    template<typename T, typename U> void f(pair<T, U>);
    

    Now, to see whether one is at least as specialized as the other one, we test the following for both function templates:

    1. chose some unique type for each template parameter, substitute that into the function parameter list.
    2. With that parameter list as argument, make argument deduction on the other template (make a virtual call with those arguments to that other template). If the deduction succeeds and there is no conversion required (adding const is such one).

    Example for the above:

    1. substituting some type X1 into T gives us some type, call it X1. argument deduction of X1 against pair<T, U> won’t work. So the first is not at least as specialized as the second template.
    2. substituting types Y1 and Y2 into pair<T, U> yields pair<Y1, Y2>. Doing argument deduction against T of the first template works: T will be deduced as pair<Y1, Y2>. So the second is at least as specialized as the first.

    The rule is, a function template A is more specialized than the other B, if A is at least as specialized as B, but B is not at least as specialized as A. So, the second wins in our example: It’s more specialized, and it will be chosen if we could in principle call both template functions.

    I’m afraid, that overview was in a hurry, i only did it for type parameters and skipped some details. Look into 14.5.5.2 in the C++ Standard Spec to see the gory details. g

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

Sidebar

Ask A Question

Stats

  • Questions 113k
  • Answers 113k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer you should change your connection string in the web.config to… May 11, 2026 at 10:07 pm
  • Editorial Team
    Editorial Team added an answer Have you looked at QNetworkAccessManager? Here's a rough and ready… May 11, 2026 at 10:07 pm
  • Editorial Team
    Editorial Team added an answer It sounds like you want to pass data to your… May 11, 2026 at 10:07 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.