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.

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
ShenDraeg
  • 167
  • 1
  • 1
  • 6
  • 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 Jan 31 '13 at 03:29

2 Answers2

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.

Rapptz
  • 20,807
  • 5
  • 72
  • 86
  • 1
    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. – ShenDraeg Jan 31 '13 at 03:40
  • 1
    *"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. – Christian Rau Jan 31 '13 at 10:25
  • @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 Jan 31 '13 at 16:16
  • 1
    @Rapptz Works fine for me without *Nov CTP*. Maybe you tried it with some pre-release beta of *VS 2012*? – Christian Rau Jan 31 '13 at 17:10
  • it would be very useful to include actual code. –  Jun 13 '22 at 10:07
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

Naps62
  • 960
  • 6
  • 17