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 7024947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:54:36+00:00 2026-05-27T23:54:36+00:00

Here is a very simple example of function specialization in C++. I think it

  • 0

Here is a very simple example of function specialization in C++. I think it should work but Visual Studio’s compiler give me an ambiguity message.

template <class T> T min(T a, T b) { 
    if(a < b) return a;
    else return b;
}

string min(string str1, string str2) { 
    if(str1.length() < str2.length()) return str1;
    return str2;
}


void main(int argc, char* argv[])
{
    int n=12, p=15;
    string str1= "monsieur", str2= "bonjour" ;

    cout << min(n,p) << endl;           
    cout << min(str1, str2) << endl;
}

In fact it says that “min(n, p) is ambigous and don’t know which function to call. It is probably a trivial problem but I haven’t figured out the problem. I tried the following headers too:

template<> string min(string str1, string str2)

and:

template<> string min<string>(string str1, string str2)

Can someone help me out?

  • 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-27T23:54:37+00:00Added an answer on May 27, 2026 at 11:54 pm

    A non-template which doesn’t require a conversion is always the better match. That is, if these were the only definitions of min(), there would be no problem (i.e. renaming the function min to something else would remove the ambiguity). The ambiguity is between template <typename T> T const& std::min(T const&, T const&) and you templatized version. Another fix is to remove the using declaration and explicitly qualify names from namespace std, i.e. the following code compiles fine:

    #include <iostream>
    #include <string>
    
    template <class T> T min(T a, T b) { 
        return a < b? a: b;
    }
    
    std::string min(std::string str1, std::string str2) { 
        return str1.length() < str2.length()? str1: str2;
    }    
    
    int main()
    {
        int n=12, p=15;
        std::string str1= "monsieur", str2= "bonjour" ;
    
        std::cout << min(n,p) << "\n";           
        std::cout << min(str1, str2) << "\n";
    }
    

    Please note that the std::string version of your min() is not a specialization but an overload! When calling the function as you did this doesn’t matter and it will choose the version as expected. However, when calling the function while explicitly specifying the template argument it will not:

    min(str1, str2);              // calls string overload
    min<std::string>(str1, str2); // calls the function template (*)
    

    (*) Actually, min<std::string>(str1, str2) is, again, ambiguous with the version from namespace std due to argument dependent look up; if it were named differently it would call the template version.

    If you wanted to really specialize the function, you would write something like this:

    template <> std::string min<std::string>(std::string, std::string) { ... }
    

    That said, here are few random notes on the program unrelated to the ambiguity and the specialization:

    • In C++ main() always has to be declared to return a int. It is allowed to omit the return statement in main() but some compilers don’t get that right. For portable code you’re best off to use a return of int and just return 0 (or EXIT_SUCCESS declared in <stdlib.h>).
    • You almost certainly want to pass these arguments by const reference: copying strings isn't that expensive, especially for small std::strings (because contemporary std::string implementations seem to use the small string optimization) but it isn't free either. If the objects are reasonably big it can be a performance problem.
    • I assume that this is just a test to play with overloading/specialization. Otherwise, the std::string version of min() is ill-advised because it behaves in unexpected ways.
    • your test strings are badly chosen because they wouldn't show that the overload/specialization is actually used.
    • std::endl is grossly overused and does something different than most people think it does: in addition to inserting a newline character ('\n') it also flushes the std::ostream. It is the latter part which comes as a surprise and I found that it creates major performance problems quite frequently. Unless you really intend to flush the stream, just write a \n.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a very simple Prototype example. All it does is, on window load, an
I guess there will be a very simple answer to this. But here goes.
Actually, I have a design question here. Its very simple but the point is:
I'm trying to do something that should be very simple, but it's causing this
Here's a very simple question. I have an SP that inserts a row into
I hope I am not missing something very simple here. I have done a
My goal here is to create a very simple template language. At the moment,
¿How would I do the operation described here , which is very simple from
i'm missing something fundamental here. i have a very simple custom class that draws
Very confused here, trying out the yuicompressor on a simple javascript file. My js

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.