Diego said:
See "7.4.1 Overloading and Return Type [fct.return]" in C++
programming language by Bjarne Stroustrup: Return types are not
considered in overload resolution.
will it appear in the new std?
No. It leads to too many ambiguities. It is present in
languages that don't have any implicit conversions.
But normal function overloading also causes ambiguity due to implicit
conversions:
void foo(unsigned);
void foo(float);
...
foo(10); // error! ambiguous call
Not as systematically. You have to supply an argument, so that
the type can be deduced. (There are also certain rules of thumb
which help avoid the problem. For example, any time you provide
an overload for any numeric type, you also provide one for int,
to avoid this sort of ambiguity.) The problem is that you can
ignore the return value, *every* type is convertible to void,
and you can't play games with the number of return types,
either. You've got a lot less possibilities for limiting the
problem.
A possible workaround is disallowing converting to void when using
overload by return-type.
There are numerous possible workarounds. The current situation
is a compromize, avoiding the worst problems without breaking C
compatibility. (The ideal solution would be to do away with
most implicit conversions.) It as a few rough edges, especially
when taking the address of a function, but while the actual
rules are so complex that almost no one understands them, the
results in practice are almost always what one would intuitively
expect, at least for real code.
[...]
Your example [of a proxy] is correct and works pretty well,
but is too boilerplate code for me.
How often do you need it?
I am sure a compiler could implement overload by return type in a
similar way:
int get();
float get();
...
float x = get(); // ok
int y = get(); // ok
double z = get(); // error. ambiguous call
get(); // error. "overloading by return type must have a 'void get();'
when the return value is discarded" (or a similar message)
And what happens when you start mixing, with differences in both
the return type and the parameter types? Or are the current
rules for function overload resolution too simple for your
taste?
Obviously, it could be done. The question is just, at what
price in terms of additional complexity, and is it worth it?
I see no reason to not having overload by return type in the language.
Complexity.
Normal overloading issues (ambiguous call) will also apply in a
similar manner.
-- Perhaps we should forward this discussion to comp.std.c++ --
You can try, but I doubt you'll find much interest in the
committee. (But that's just my feeling---I could be wrong about
it.)