r/programminghelp • • 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.65f

However, I don't understand why this was even necessary.

It also compiled fine before I added the bool debug parameter.

Can someone explain?

4 Upvotes

7 comments sorted by

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 = false to 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 f suffix, 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 true the second function would fire without issue.

1

u/PageFault Sep 03 '21

Why is it allowed to be converted to a float* when it is a float[3]?

I normally cannot to that.

#include <cstddef>

int main( void )
{
   float *xyz;
   xyz = NULL;

   float hpr[3];
   hpr = NULL;
}

Compiling gives:

> g++ test.cpp 
In file included from /usr/include/c++/8/cstddef:50,
                 from test.cpp:1:
test.cpp: In function ‘int main()’:
test.cpp:9:10: error: incompatible types in assignment of ‘long int’ to ‘float [3]’
    hpr = NULL;

I'm not arguing. You are obviously correct. I'm just trying to understand.

2

u/[deleted] Sep 03 '21 edited Sep 03 '21

An array is considered to be the same thing as a pointer to the first item in the array.

#include <iostream>

void printFloat(float* f);

int main( void ) { 
    float abc[3] = { 3.1, 4.2, 5.3 };
    printFloat(abc);
}
void printFloat(float* f) {
    std::cout << *f << std::endl;
}

EDIT: Rather, this might help illustrate it better:

#include <iostream>

void printFloat(float* f);

int main( void ) { 
    float abc[3] = { 3.1, 4.2, 5.3 }; 
    float def[2] = { 6.4, 7.5 }; 
    float * other = new float(3.2f);

    printFloat(abc); 
    printFloat(def); 
    printFloat(other);
}

void printFloat(float f[3]) { 
    std::cout << *f << std::endl; 
}

Results:

3.1
6.4
3.2

1

u/PageFault Sep 03 '21

Forgive me, I think I'm having trouble finding the proper words for my question.

If an array is considered to be the same thing as a pointer to the first item in the array, then why isn't this valid?

float hpr[3];
hpr = NULL;

I appreciate you having patience with me.

1

u/[deleted] Sep 03 '21 edited Sep 03 '21

float hpr[3];

hpr = NULL;

NULL is a null pointer constant.

hpr is an Array and cannot be assigned to a null pointer constant

hpr[0] is a pointer, so it can be assigned a null pointer constant.

#include <iostream>

int main() { 

    float hpr[3]; 
    float * hpr2; 

    std::cout << hpr << std::endl;
    
    std::cout << hpr[0] << std::endl;
    hpr[0] = NULL;
    std::cout << hpr[0] << std::endl;

    std::cout << hpr2 << std::endl;
    hpr2 = NULL;
    std::cout << hpr2 << std::endl;
}

EDIT: I think the confusion arises because your definition has an array as the first parameter. Just because your function asks for an array of 3 floats, doesn't mean the compiler cares. As far as it is concerned, it's looking for a float pointer.

#include <iostream>

void testFunction(float floaty[3]);
    
int main() { 
    float hpr4[4] = {1.1,2.2,3.3,4.4};
    float hpr3[3] = {1.1,2.2,3.3};
    float hpr2[2] = {1.1,2.2};
    
    testFunction(hpr4);
    testFunction(hpr3);
    testFunction(hpr2);
}

void testFunction(float floaty[3]) {
    std::cout << floaty[0] << std::endl;
}

Results:

1.1
1.1
1.1

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))