How is the call of this function ambiguous in C ++?

Consider the 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';
}

The compiler reports the following error:

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

But I do not understand why and how it is ambiguous? Does this include any automatic type promotion? I know that calling fun (3L) makes compilation successful.

+4
source share
1 answer

Probably 3 can be interpreted as other types (for example char, unsigned...), so it may be ambiguous to know what function you want to call the compiler. You need to specify a value of 3: 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';
}
+2

Source: https://habr.com/ru/post/1612525/


All Articles