0
//assume there is no Print(double dValue)
void Print(unsigned int nValue);
void Print(float fValue);
Print(3.14159);

Shouldn't the Print(3.14159) match with Print(float) ?

Instead of it, this code lead to an ambiguous match

  • Is 3.14159 a double ?
  • How to differentiate between float and double ?
Juen Khaw
  • 161
  • 1
  • 1
  • 10
  • What do you mean with ambiguous match? Do you get compiler error or strange behaviors? – gcswoosh Apr 05 '15 at 00:45
  • Check this out: http://stackoverflow.com/questions/2386772/difference-between-float-and-double – sam Apr 05 '15 at 00:45

2 Answers2

3

Is 3.14159 a double ?

Yes, it is.

How to differentiate between float and double ?

Use 3.14159f to make the constant a float. Use 3.14159 to make the constant a double.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

3.14159 numeric literal is a double, not a float. C++ has two choices:

  • Convert double to unsigned int, and call the first overload
  • Convert double to float, and call the second overload

Both choices require the same number of conversions, to C++ issues an error.

You can fix this by appending F to the end of the literal:

Print(3.14159F);
//           ^

Now the first choice still requires a float to unsigned int conversion, while the second choice can proceed without conversion; hence, the second overload "wins".

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523