ambiguous call to overloaded function - int and int&

后端 未结 3 1784
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 03:44
void g(int& a)
{
    std::cout<<\"int&\\n\";
} 

void g(int a)
{
    std::cout<<\"int\\n\";
}  

int main()
{    
    int a = 2;   
    g(a); //w         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-07 04:41

    If you can cast, there is, of course a way to disambiguate the call:

    g(const_cast(a));
    

    If you insist to call the reference version, the resolution is a bit more tricky:

    static_cast(g)(a);
    

提交回复
热议问题