Overloading functions -- int& vs. int arguments

Hi all,

I started learning C++ in September and am studying up for an exam on Monday.
The correction to a past exam says these two function declarations can cohabitate in a program:
void fct(int&);
void fct(int);

But if you call them in the main, say with...
int i(15);
fct(i);

..how does the compiler know whether to modify i (&) or to modify a copy of i?

Thanks for your help!
Alex
Technically, you're allowed to do that. However, I can't possibly think of a time when the referenced version could be called. Passing a constant int would result in the overloaded function to be called. Attempting to pass a non constant variable results in an error. See here:
1
2
3
4
5
6
7
8
9
10
void fct(int &n){}
void fct(int n){}

int main() {
   int i(15);
   fct(i); // error: call of overloaded 'fct(int&)' is ambiguous
   fct(15); // ok
   
   return 0;
}


Like I said, I couldn't begin to imagine when a referenced version would be used over the other one, but it does compile as long as you pass a constant value to the function, otherwise it has no clue.

Note: You mentioned it was a correction. I'm assuming your teacher didn't know that either and someone pointed it out to them. Have you asked your teacher to elaborate?
Hi,

Thanks for your reply.

The teacher is unavailable, but the consensus among students is that the question was just whether or not the declaration is valid, and did not concern the implementation and usage thereafter. I suppose it's an evaluation of only our theoretical understanding.

You've cleared it up for me; thanks a lot!
Alex
> .how does the compiler know whether to modify i (&) or to modify a copy of i?

It does not. However, the compiler can distinguish between reference to int and reference to const int :
1
2
void fct( int& ) ;
void fct( const int& ) ;


For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

void foo( int& ) { std::cout << "foo: l-value reference to int\n" ; }
void foo( const int& ) { std::cout << "foo: l-value reference to const int\n" ; }

void bar( int& ) { std::cout << "bar: l-value reference to int\n" ; }
void bar( const int& ) { std::cout << "bar: l-value reference to const int\n" ; }
void bar( int&& ) { std::cout << "bar: r-value reference to int\n" ; }

int main()
{
    int i = 7 ;
    const int c = i ;

    foo(i) ; // foo: l-value reference to int
    foo(c) ; // foo: l-value reference to const int
    foo(15) ; // foo: l-value reference to const int

    bar(i) ; // bar: l-value reference to int
    bar(c) ; // bar: l-value reference to const int
    bar(15) ; // bar: r-value reference to int
}
Topic archived. No new replies allowed.