+ 1
I am using function overloading. But why am I getting 'ambiguous call error' when calling the 1st function? How is it ambiguous?
6 Respostas
+ 2
First one can't decide which function should be called ,the one with two float values or the one with two int values on 3rd and 4th place as default and 1st and 2nd parameter being int for both
+ 1
sum(10,15,0,0); will call 1st function.
0
sum(10, 15) ;
This will match to both overlaoeded functions.. Which one do you want to call here..?
0
Jayakrishna🇮🇳
Say I want to call 1st function. Is there any way to call either of the two functions succesfully and not get error, keeping both the funtion declarations as it is?
0
Prasenjit Kumar Yes using a C++20 feature - concepts!
template<typename T>
T sum(T x, T y,T w=0,T z=0)
requires is_same_v<int,T>
{
return w+x+y+z;
}
template<typename T>
T sum(T x, T y,T w=0,T z=0)
requires is_same_v<float,T>
{
return w+x+y+z;
}
0
The compiler is ignoring the default values and takes the declared variable as parameter where the two functions are same