Skip to main content
  1. About
  2. For Teams
Asked
Viewed 25k times
14

In attempting to insert integer values into a string, I thought that my prayers were answered when I found std::to_string, but for some reason whenever I actually try to use it, Visual Studio complains about ambiguity. Here is the current incarnation of my function:

string get_time_remaining (int elapsed)
{
    string remaining;
    string temp_string;

    int time_remaining = TimeLimit - elapsed;

    int temp_int;

    temp_int = int(time_remaining / 3600);

    if(temp_int == 0)
        remaining = "00 : ";
    else
    {
        temp_string = std::to_string(temp_int);   // Here!
        remaining = temp_string + " : ";
    }

    temp_int = time_remaining % 60 + 1;

    if(temp_int < 10)
        remaining = remaining + "0";

    temp_string = std::to_string(temp_int);

    remaining = remaining + temp_string;

    return remaining;
}

I have tried casting temp_int inside the call to to_string, and as you can see I even tried casting the result of what should be integer division, but no matter what I do, VS spits this out at me:

d:\my programs\powerplay\powerplay\powerplay.cpp(1285): error C2668: 'std::to_string' : ambiguous call to overloaded function
1>          d:\microsoft visual studio 10.0\vc\include\string(688): could be 'std::string std::to_string(long double)'
1>          d:\microsoft visual studio 10.0\vc\include\string(680): or       'std::string std::to_string(_ULonglong)'
1>          d:\microsoft visual studio 10.0\vc\include\string(672): or       'std::string std::to_string(_Longlong)'

Any help would be appreciated.

1
  • I should also point out there is a matching error for the second call to to_string as well, so it's not limited to just one call.
    ShenDraeg
    –  ShenDraeg
    2013-01-31 03:29:16 +00:00
    Commented Jan 31, 2013 at 3:29

2 Answers 2

25

MSVC11 lacks the proper overloads for std::to_string so you have to do a static_cast to unsigned long long or long long

Note that this bug is fixed in the November CTP 2012. Which you can get here.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much. Casting it to "not-int" allowed it to compile. I'll mark it as answer in 2 minutes, when it lets me do that.
"Note that this bug is fixed in the November CTP 2012" - It should already be fixed in the plain VS 2012, especially since the November CTP didn't bring any library changes at all. This also makes your tagging of his question to visual-studio-2012 invalid, since he obviously uses 2010, as can be seen from his compiler output.
@ChristianRau thank you for catching that -- however on my VS2012 without November CTP I had the same issue with the overloads hence the likely confusion on my behalf.
@Rapptz Works fine for me without Nov CTP. Maybe you tried it with some pre-release beta of VS 2012?
user2261062
user2261062
it would be very useful to include actual code.
3

temp_int is a int value, and Visual Studio seems to detect only overloads which receive either double, long long or unsigned long long values, so it doesn't know which overload to use, thus the ambiguity (although it would seem intuitive to cast integer to long values)

Either declare temp_int as a long long, or cast it when invoking the function

2 Comments

It's unsigned long long and long long not long.
And it's long double, not double.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.