Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguity of function overloading - Integers vs. Doubles

Tags:

ambiguity

Suppose I wish to have 2 functions, one that generates a random integer within a given range, and one that generates a random double within a given range.

int GetRandomNumber( int min, int max );
double GetRandomNumber( double min, double max );

Notice that the method names are the same. I'm trying to decide whether to name the functions that or...

int GetRandomInteger( int min, int max );
double GetRandomDouble( double min, double max );

The first option has the benefit of the user not having to worry about which one they are calling. They can just call GetRandomNumber with integers or doubles and get a result.

The second option is more explicit in the names, but it reveals unneeded information to the caller.

I know this is petty, but I care about petty things.

Edit: How would C++ behave regarding implicit conversion.

Example:

GetRandomNumber( 1, 1 ); 

This could be implicitly converted for the GetRandomNumber function for the double version. Obviously I don't want this to occur. Will C++ use the int version before the double version?

like image 253
Anonymous Avatar asked Dec 17 '09 21:12

Anonymous


People also ask

Where can an ambiguity occur in overloading a function?

In Function overloading, sometimes a situation can occur when the compiler is unable to choose between two correctly overloaded functions. This situation is said to be ambiguous. Ambiguous statements are error-generating statements and the programs containing ambiguity will not compile.

What is ambiguity in function overloading?

When the compiler is unable to decide which function it should invoke first among the overloaded functions, this situation is known as function overloading ambiguity. The compiler does not run the program if it shows ambiguity error.

What is function overloading How are function calls matched with overloaded functions explain with the help of an example?

Function Overloading in C++When a function name is overloaded with different jobs it is called Function Overloading. In Function Overloading “Function” name should be the same and the arguments should be different. Function overloading can be considered as an example of a polymorphism feature in C++.

How are overloaded functions differentiated?

Overloaded functions differentiate between argument types that take different initializers. Therefore, an argument of a given type and a reference to that type are considered the same for the purposes of overloading. They're considered the same because they take the same initializers.


1 Answers

I prefer your second example, it is explicit and leaves no ambiguity in interpretation. It is better to err on the side of being explicit in method names to clearly illuminate the purpose and function of that member.

The only downside to your approach is that you have coupled the name of the method to the return type which is not ideal in the event that you want to change the return type of one of these methods. However in that case I would be better to add a new method and not break compatibility in your API anyways.

like image 130
Andrew Hare Avatar answered Sep 21 '22 01:09

Andrew Hare