r/programminghelp • u/PageFault • Sep 03 '21
C++ Why does changing my define from float->double make my call ambiguous?
Here is the source:
> cat ambiguous.cpp
#include <iostream>
#define MY_DEFINE -0.65 //call of overloaded ‘calcPos(float [3], NULL, float [3], float [3], int, int, double)’ is ambiguous
//#define MY_DEFINE -0.65f //Compiles
void calcPos( float xyzDst[3], float hprDst[3],
const float xyzSrc[3], const float hprSrc[3],
float ox, float oy, float oz, float oh_=0.0f, float op_=0.0f, float or_=0.0f );
void calcPos( float xyzDst[3], float hprDst[3],
const float xyzSrc[3], const float hprSrc[3],
const float xyzOffset[3], const float hprOffset[3], bool debug=false );
int main( void )
{
float xyz[3];
float hpr[3];
calcPos( xyz, NULL, xyz, hpr, 0, 0, MY_DEFINE );
}
void calcPos( float xyzDst[3], float hprDst[3],
const float xyzSrc[3], const float hprSrc[3],
float ox_, float oy_, float oz_,
float oh_, float op_, float or_ )
{
return;
}
void calcPos( float xyzDst[3], float hprDst[3],
const float xyzSrc[3], const float hprSrc[3],
const float xyzOffset[3], const float hprOffset[3], bool debug )
{
if ( debug )
{
std::cout << "Debug Info" << std::endl;
}
return;
}Here is the error:
> g++ ambiguous.cpp
ambiguous.cpp: In function ‘int main()’:
ambiguous.cpp:19:54: error: call of overloaded ‘calcPos(float [3], NULL, float [3], float [3], int, int, double)’ is ambiguous
calcPos( xyz, NULL, xyz, hpr, 0, 0, MY_DEFINE );
^
ambiguous.cpp:6:10: note: candidate: ‘void calcPos(float*, float*, const float*, const float*, float, float, float, float, float, float)’
void calcPos( float xyzDst[3], float hprDst[3],
^~~~~~~
ambiguous.cpp:10:10: note: candidate: ‘void calcPos(float*, float*, const float*, const float*, const float*, const float*, bool)’
void calcPos( float xyzDst[3], float hprDst[3],
^~~~~~~I solved the error by setting:
#define MY_DEFINE -0.65fHowever, I don't understand why this was even necessary.
It also compiled fine before I added the bool debug parameter.
Can someone explain?
2
u/KuntaStillSingle Sep 03 '21
My only guess is the compiler doesn't know whether to cast the ints and double to a (const float*) or a float. If that's the case it strikes me as pretty unintuitive, you would think it would assume float in both cases.
So with the caveat I'm not entirely sure the explanation if you convert one of the 0s to 0.0f it will disambiguate.
calcPos( xyz, hpr, xyz, hpr, 0.0f, 0, MY_DEFINE);1
u/ConstructedNewt MOD Sep 03 '21
The compiler needs to know which function to call for the line in question. As double is neither float nor bool the compiler cannot infer which method named calcPos to choose. This may be confusing as, you can use a double in stead of a float, seamlessly. But this is true (in C) for bool as well (which is interchangeable with any number (as the number's 0 byte-value or not))
1
u/[deleted] Sep 03 '21
-0.65 and -0.65f can both be implicitly cast to a bool.
-0.65 and -0.65f can both be implicitly cast to a float.
When you add
bool debug = falseto the second function, this means the first function has 6 parameters with 3 optional, and the second has 5 parameters with 1 optional (6).When you call the function in your main, you pass 6 values. Since the last value can be cast to either a bool or a float, and you're not explicitly making it a float with
fsuffix, the compiler doesn't know which to cast it to so it doesn't know which function to use, making the call ambiguous.If you changed it to
#DEFINE MY_DEFINE truethe second function would fire without issue.