We all know that you can overload a function according to the parameters:
int mul(int i, int j) { return i*j; }
std::string mul(char c, int n) { return std::string(n, c); }
Can you overload a function according to the return value? Define a function that returns different things according to how the return value is used:
int n = mul(6, 3); // n = 18
std::string s = mul(6, 3); // s = "666"
// Note that both invocations take the exact same parameters (same types)
You can assume the first parameter is between 0-9, no need to verify the input or have any error handling.
No.
You can't overload by return value because the caller can do anything (or nothing) with it. Consider:
mul(1, 2);
The return value is just thrown away, so there's no way it could choose an overload based on return value alone.
OK you geniuses ;) this is how you do it like a pro.
use like
no stupid <>
You could do something like
And then call it like this:
There is no way of overloading on the return value.
Let mul be a class, mul(x, y) its constructor, and overload some casting operators.
I presume you could have it return some weird type Foo that just captures the parameters and then Foo has an implicit operator int and operator string, and it would "work", though it wouldn't really be overloading, rather an implicit conversion trick.
Short and simple, the answer is NO. In C++ the requirements are:
1: name of functions MUST be the same
2: set of arguments MUST differ
*The return type can be the same or different