Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous call to overloaded function - std::to_string [duplicate]

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.

like image 975
ShenDraeg Avatar asked Jan 31 '13 03:01

ShenDraeg


2 Answers

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.

like image 108
Rapptz Avatar answered Oct 31 '22 01:10

Rapptz


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

like image 33
Naps62 Avatar answered Oct 31 '22 00:10

Naps62