Topic: Overloaded function resolution for const
Author: Daniel.Edelson@inria.fr (Daniel R. Edelson)
Date: 19 Feb 92 15:52:05 GMT Raw View
In this article, my language definition quotations and reference
citations are to the reference manual found in the
back of ``The C++ Programming Language'', second edition,
by Stroustrup, because I don't have a ANSI working document with me
(left it at home). If the current draft resolves this then
I apologize for taking up net bandwidth.
Consider calling an overloaded function for which there
are two implementations:
f(void *) { }
f(const void *) { }
According to the rules in Stroustrup's reference manual, a call to
these functions with an object pointer is ambiguous. For example,
calling f(pi) where pi is an (int*) is ambiguous. Is there a good
reason for not allowing the conversion to (void*) to be preferred for
pointers to non-const objects, and the pointer to (const void *)
to be preferred for pointers to const objects?
Here's an example. Cfront 3.0 correctly rejects the following code
claiming that the call to the overloaded constructor is ambiguous.
(According to the document I've cited.) However, I haven't yet been
able to come up with a good reason why this needs to be the case.
I would be grateful if someone could provide one.
struct foo {
foo(void *) { }
foo(const void *) { }
};
int main(void)
{
int * pi = 0;
foo f(pi); // ambiguous?
return 0;
}
The two feasible conversion sequences for the actual argument are:
a) int * ==> void * (match)
and
b) int * ==> void * ==> const void * (match)
I agree that you can't simply consider the first sequence better
because it's shorter. However, it seems like you should be able to
favor conversion sequences that do not add const where it did
not before exist over those that do. Is this incorrect?
Thanks,
Daniel R. Edelson
edelson@sor.inria.fr, and, daniel@cse.ucsc.edu
Author: jss@lucid.com (Jerry Schwarz)
Date: Wed, 19 Feb 92 21:26:51 GMT Raw View
In article <3261@seti.UUCP>, Daniel.Edelson@inria.fr (Daniel R. Edelson) writes:
>
|> I agree that you can't simply consider the first sequence better
|> because it's shorter. However, it seems like you should be able to
|> favor conversion sequences that do not add const where it did
|> not before exist over those that do. Is this incorrect?
|>
Judgements in this area are sometimes slippery.
Consider
void f(void*) ; //variant 1
class X ;
void f(const X*) ; //variant 2
... X* p ; f(p) ; ...
Right now the call is unambiguously resolved to variant 2, which
I believe is the clearly correct resolution.
Under the proposed revision would this be regarded as variant 1
or ambiguous?
-- Jerry Schwarz