views:

194

answers:

6

Can I overload a function which takes either a reference or variable name?

For example when I try to do this:

void function(double a);
void function(double &a);

I would like the caller of this function to be able to do:

double a = 2.5;
function(a); // should call function(double &a)
function(2.3); // should call function(double a)

I would like to write pass-by-reference functions for better memory use and possible manipulation of the variable outside of scope, but without having to create a new variable just so I can call the function.

Is this possible?

Cheers

A: 

I don't think the definition:

void function(double a);
void function(double &a);

is possible... How would the compiler know which function to call for function(x)? If you want to use both by Reference and by Value, you should use two different functions. This would also make the code much easier to read, than this ambiguous overload.

Tapdingo
I think it is possible, and the reference-version will be preferred if possible. Haven't got anything to back that claim though.
roe
Guess i'll try it out with different compilers...
Tapdingo
Tapdingo
@roe - The definition itself is possible. However, you won't be able to call the reference version due to overload resolution ambiguity. There is nothing in the Standard that makes the reference version preferable. Non-reference version can be called with a const or rvalue parameter.
atzz
+1  A: 

I tried it, and it failed

At least in MSVC2008, this is not possible - and I think this applys to all c++ - Compilers.

The definiton itself is valid, but when trying to call the function

function(a);

with a variable as a parameter, an compiler error is raised as the compiler is unable to decide which function to use.

sum1stolemyname
+7  A: 

I think you're missing the point here. What you really should have is JUST this:

void function(const double &a);

Note the "const". With that, you should always get pass-by-reference. If you have non-const pass by reference, then the compiler will correctly assume that you wish to modify the passed object - which of course is conceptually incompatible with the pass-by-value variant.

With const references, the compiler will happily create the temporary object for you behind your back. The non-const version doesn't work for you, because the compiler can only create these temporaries as "const" objects.

Roddy
+1, good call..
roe
Luther Blissett
Indeed. The convention in C++ is usually to pass built-in types (int, double, char, pointers, etc) by value, and composite types (classes and structs) by reference.
Tyler McHenry
@Tyler, Luther. You are of course both correct. I'm assuming that the choice of 'double' is just a type chosen as a (slightly poor) example...
Roddy
A: 

No, you cannot do this coz both the functions will effectively produce the same mangled names and the linker would be unable to resolve the function call.

Taran9
Uhm. No. A reference parameter will not produce the same name mangling as a normal (copied parameter). That would be equivalent to say that a pointer-to-double and a double parameter would both yield the same name mangling.
roe
+1  A: 
void function(double const &a);  // instead of void function(double a);
void function(double       &a); 
Alexey Malistov
A: 

There is no benefit of passing POD-types by reference, as they are almost always (always?) passed in a registry. Complex types (ones that consist of more than just one POD-variable) passed by const reference is usualy (always?:D) a prefferable way over a copying and stack push. You even can controll the creation of temporaries of your complex-typed variables from POD-types by creating explicit constructors

Dark