Function Overloading refuses to work with float type arguments -- why?

As an exercise on function overloading concept, I was playing with the following sketch in UNO for adding two interger numbers and two floating point numbers with the same function name (add). Unfortunately, the sketch works well with double type for the float numbers but not with float type. Would appreciate to hear the reasons from the Forum.

This is the compilation error message:
call of overloaded 'add(double, double)' is ambiguous

void setup()
{
  Serial.begin(9600);
  int resint = add(8, 5);
  float resf = add(3.41, 7.54);
  Serial.println(resint);
  Serial.println(resf, 2);
}

void loop()
{
  
}

int add(int x, int y)
{
  return (x + y);
}

float add(float x, float y)
{
  return (x + y);
}

How about like that?

If you use the 'f' suffix, then your float constants really are float constants but without, the constants inherently have a type 'double'.

so you could also declare

double add(double x, double y) {
  return (x + y);
}

and be fine (as float do promote to double without a loss - but the result then can be a question)

Works so well!

Why does my sketch work without the use of the overload keyword?
The textbook says:
"The C++ compiler must be told in advance, using the keyword overload, that a function is going to be overloaded." Accordingly I tried to add the following line in the global space; however, it is not accepted by the compiler and eventually, the sketch works without this line.

overload add;

see answer #3

you could also do

template<typename T> 
const T add( const T& A, const T& B ) { // for simple numbers A and B you can get rid of the &
    return A + B; 
}

void setup()
{
  Serial.begin(9600);
  int resint = add(8, 5);
  float resf = add(3.41, 7.54);
  Serial.println(resint);
  Serial.println(resf, 2);
}

void loop() {}

and let the compiler build the functions you need :slight_smile:

1 Like

Your trick works well without appending f with the actual arguments.

Can you please, briefly describe the working principle of the following function? I understand that the function is receiving the addresses of the actual arguments from the calling function. After that what happens?

template<typename T> 
const T add( const T &A, const T &B ) 
{ // for simple numbers A and B you can get rid of the &
    return A + B; 
}

forget the & for the time being, that's just if A and B were using a more complex type like an instance of a matrix etc, to avoid creating a copy of the instance and work on the original 'by reference')

what you define is called a template. You tell the compiler to build on demand a function when needed based on the types used in the code.

for example when you call add(8, 5); the compiler sees you need to call the function with 2 int. It matches the template (the conceptual type is 'T' which will be replaced by int) and so the compiler generates and compile

const int add( const int A, const int B ) { 
   return A + B; 
}

then you call add(3.41, 7.54). here the compiler sees two double (because you did not use the f suffix) and so generates and compile

const double add( const double A, const double B ) { 
   return A + B; 
}

and this will be the function called.

➜ the template lets you define basically all the overloaded variant in one go and you let the compiler decide which one makes sense to generate

2 Likes

What textbook?

The C++ Reference doesn't list "overload" as a keyword.
https://en.cppreference.com/w/cpp/keyword

There is an "override" keyword but it's only used on virtual member functions.

C
The Complete Reference, Second Edition, 1990
By: Herbert schildt

Page - C++ 789

There is also an Example:

overload power;
int power(intb, int e);
double power(double b, itn e);
......................

That’s a book from last millenium :slight_smile:
See overloading - why overload keyword removed from C++? - Stack Overflow

1 Like

I'm sure a lot of the examples you find in there won't even compile now at least not under C++ For example this:

double old_style();           /* Obsolete function declaration */

double alt_style( a , real )  /* Obsolete function definition */
    double *real;
    int a;
{
    return ( *real + a ) ;
}

I have stack of IT books of that vintage on the bookshelves and the only reason i don't throw them away is to prevent my wife misusing the space for something else.

1 Like

In 1991, that was the hard copy book for me to practice and understand some of the syntax and semantic rules of the C Language (course work) under Turbo C . Now-a-days, there are huge tutorials in the net from various sources and yet I consult that book time-to-time as it contains few definitions of my liking -- for example: Constructor Function.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.