Assuming we have this example of functions in C++
void foo(int x) { std::cout << "foo(int)" << std::endl; }
void foo(int& x) { std::cout << "foo(int &)" << std::endl; }
Is there a possibility to differentiate which function I want to call by making some modification to the call arguments?
If function foo is called in any of these ways:
foo( 10);
i = 10;
foo( static_cast<const int>(i));
foo( static_cast<const int&>(i)); // Aunque aquí convertir un tipo básico a través de una referencia no tiene mucha utilidad. Sólo es para exponer los casos posibles.
Calls the first of the two overloaded functions, because a constant argument cannot be passed by reference to a non-constant parameter. But how would one go about calling the second of the overloaded functions? If I make the following invocation:
int i = 10;
foo( i);
An ambiguity error occurs since both functions are valid for this argument.
In this link https://stackoverflow.com/a/5465379/6717386 it states that one way to solve it is, in the case of handling objects instead of basic types, to make the copy constructor private so that, not being able to make a copy of the value, you have to call the second function and pass the object by reference. But what about with basic types is there a way? Is it obligatory to change the name of the functions to avoid this type of problem?
That's right, C++ has NO mechanism to distinguish between call by copy and call by reference, so the answers to what about basic types is there a way? it would be a simple no.
But it's a very boring answer, so we'll try to use language tools to try to accomplish the goal:
We can modify the behavior and bypass the ambiguity by using the unary operator
+
:Applying the unary operator
+
on a type returns its value, so the only option available is callingfoo
by copy (goodbye ambiguity!)... unfortunately this doesn't solve calling version by non-constant reference.The only way to achieve this is to make one of the two options better than the other in case of ambiguity, for example by making one of the functions a template:
When the compiler chooses between a template version and a non-template version, the non-template version is the better choice, so the ambiguity is gone; Unfortunately, this last option implies that " It is obligatory to change the name of the functions to avoid this type of problem ", understanding "name of the functions" by their type.