overload toy example

Hi guys,

I was playing with a toy example to overloading functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

void f(int x){std::cout<<"first overload"<<std::endl;}
void f(int& x){std::cout<<"second overload"<<std::endl;}
void f(const int& x){std::cout<<"third overload"<<std::endl;}

int main(){

int b = 3;
f(b);

return 0;
}


Now, this won't compile due to ambiguity.
If I instead have
1
2
3
//void f(int x){std::cout<<"first overload"<<std::endl;}
void f(int& x){std::cout<<"second overload"<<std::endl;}
void f(const int& x){std::cout<<"third overload"<<std::endl;}

it will compile and the second function is executed. So the second function takes precedence over the third, probably because my input argument was not declared as constant.

If I have
1
2
3
void f(int x){std::cout<<"first overload"<<std::endl;}
void f(int& x){std::cout<<"second overload"<<std::endl;}
//void f(const int& x){std::cout<<"third overload"<<std::endl;} 

it will not compile, so the first two functions seem to have the same preference.

Due to the fact that function 1 can't be distinguised from 2 and that 2 is preferred over 3, it should follow that if 2 is commented out, 1 should be preferred over 3:
1
2
3
void f(int x){std::cout<<"first overload"<<std::endl;}
//void f(int& x){std::cout<<"second overload"<<std::endl;}
void f(const int& x){std::cout<<"third overload"<<std::endl;}

But this is ambiguous again...

This looks illogical to me. Any comments?
Hello PhysicsIsFun,

Have a look at https://www.learncpp.com/cpp-tutorial/function-overloading/

An overloaded function is based on different paramenters for the same function name.
So,
1
2
3
void f(int x){std::cout<<"first overload"<<std::endl;}
void f(int& x){std::cout<<"second overload"<<std::endl;}
void f(const int& x){std::cout<<"third overload"<<std::endl;}

All define the same type of parameter. Version 2 and 3 still define an "int" even though it is a reference to an "int" defined in "main".

To use an overloaded function would be:
1
2
void f(int x)
void f(double x)

Since the parameters are different the compiler will know which function to call based on what is sent to the function.

Andy
For something to read to put you to sleep, have a look at https://en.cppreference.com/w/cpp/language/overload_resolution which explains how it all works.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using std::cout;

//int f(      int  v) { cout << "f(int)\n";  return v; }
int f(      int& v) { cout << "f(int&)\n"; return v; }
int f(const int& v) { cout << "f(const int&)\n"; return v; }

int main() {
    int x = 1;
    const int y = 2;
    f(x);
    f(y);
}

                        MATCH WHEN CALLING WITH
                        int         const int
DEFINED FUNCS           ==========  ===========
int                     int         int
     int&               int&        NOMATCH
           const int&   const int&  const int&
int; int&               AMBIG       int
int;       const int&   AMBIG       AMBIG
     int&; const int&   int&        const int&
int; int&; const int&   AMBIG       AMBIG

You would normally have either only f(int) or both f(int&) and f(const int&).
Having f(int) along with either or both of the others is a problem.
Thanks for the responses!

@Handy Andy, I know how it is done properly, I was just wondering about the behavior in my case.

@seeokus, this really seems like a tedious topic, I will probably not go through it in detail, but thanks for giving me the term "overload resolution".

@dutch, thanks for the table. How did you create it or where did you find it?

thanks for the table. How did you create it or where did you find it?

I made it by running the given code with all those combinations.
Registered users can post here. Sign in or register to post.