Topic: Lvalue overloading?
Author: daveg@synaptics.com (David Gillespie)
Date: 8 Jun 92 22:42:18 GMT Raw View
I notice that page 318, section 13.2 of ARM states that
"A temporary variable is needed for a formal argument
of type T& if the actual argument is not an lvalue, has
a type different from T, or is a volatile and T isn't.
This does not affect matching. It may, however, affect
the legality of the resulting match ..."
Why is this? Without this rule, it would be possible to overload
a function based on whether or not the argument is an lvalue. I
claim that this would be a useful feature. Does anybody know of
a good reason why it is not allowed?
void foo(int &);
void foo(const int &);
void bar()
{
int i;
foo(i);
foo(12);
foo(i+1);
}
In the first case, foo(int &) is unambiguously called because of
the rule that T&-to-const T& is less preferred than otherwise.
In the second and third cases, since the argument is not an lvalue
only foo(const int &) would match, so it would be unambiguously called
(if the above rule were relaxed, that is).
Both gcc 2.1 and Sun C++ 2.0 accept the above file without errors
or warnings, and generate three calls to foo(int &) and none to
foo(const int &). If I change to "const int i;" in the above, they
still generate three calls to foo(int &)! The ARM specifically states
(page 308) that functions may be overloaded on the distinction between
T& and const T&, but these compilers don't seem to do anything useful
with this fact.
Thanks,
-- Dave
--
Dave Gillespie
daveg@synaptics.com, uunet!synaptx!daveg
or: daveg@csvax.cs.caltech.edu