Destructor
Destructor

Reputation: 14438

How this function call is ambiguous in C++?

Consider following program: (See live demo here http://ideone.com/7VHdoU )

#include <iostream>
void fun(int*)=delete;
void fun(double)=delete;
void fun(char)=delete;
void fun(unsigned)=delete;
void fun(float)=delete;
void fun(long int);
int main()
{
    fun(3);
}
void fun(long int a)
{
    std::cout<<a<<'\n';
}

Compiler is giving following error:

error: call of overloaded 'fun(int)' is ambiguous
  fun(3);
       ^

But I don't understand why & how it is ambiguous? Does it involve any kind of automatic type promotion here? I know that calling fun with (3L) makes compilation successful.

Upvotes: 4

Views: 4455

Answers (1)

Krypton
Krypton

Reputation: 3325

Probably 3 can be interpreted as other types (like char, unsigned...), so it might be ambiguous for the compiler to know what function you want to call. You need to indicate value 3 is a long int.

#include <iostream>
void fun(int*)=delete;
void fun(double)=delete;
void fun(char)=delete;
void fun(unsigned)=delete;
void fun(float)=delete;
void fun(long int);
int main()
{
    fun((long int)3);
}
void fun(long int a)
{
    std::cout<<a<<'\n';
}

Upvotes: 2

Related Questions