I know this will be simple for someone, but I would appreciate some insights as I don't quite understand what's going on.
I'm just playing with some code I created, and added two functions, per the code below. But, despite explicitly expressing each value in the function calls with L (long) and F (float) the compiler vomits it back up, telling me "...call of overloaded 'calc(long double)' is ambiguous".
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int calc(long x) {
return x * 2;
}
int calc(float y) {
return y * 2;
}
int main() {
cout << calc(100.0L) << calc(1.5F) << endl;
return 0;
}
In function 'int main()':
13:20: error: call of overloaded 'calc(long double)' is ambiguous
13:20: note: candidates are:
4:5: note: int calc(long int)
8:5: note: int calc(float)
Could somebody explain exactly why it doesn't work as I'm thinking it should do? Much appreciated! Thanks.
The literal 100.0 has type double.
If you add the "L" suffix to a double literal, you get a long double literal.
The error occurs because the compiler cannot decide whether to squeeze a long double into a float, or squeeze long double into a long. Either conversion throws away information, because there are values of long double that cannot be represented by float or long int. Such conversions are called narrowing conversions.
If you want a variable with type long, say 100l instead of 100.0l.