Re: strange looking ambiguity with type cast
Re: strange looking ambiguity with type cast
- Subject: Re: strange looking ambiguity with type cast
- From: GoochRules! <email@hidden>
- Date: Wed, 11 Feb 2004 14:02:39 -0700
On Feb 11, 2004, at 1:19 PM, jean-frangois gauthier wrote:
(please point me to another list if this is not the right place to
discuss this) it seems to me the following should just compile:
class a
{
public :
a (int) ;
} ;
class b
{
public :
operator a (void) ;
operator int (void) ;
} ;
int main (int, char ** const)
{
b _b ;
(a) _b ;
}
but gcc complains:
error: call of overloaded `a(b&)' is ambiguous
error: candidates are: a::a(const a&)
error: a::a(int)
why? this compiles fine with cwpro and msdev. it compiles if i replace
(a) _b with _b. operator a ().
To me it looks like gcc is finding two possible user-defined conversion
operators to create an 'a' from a 'b' because there's a default copy
constructor for a (the first candidate), and the overloaded constructor
you provided (the second candidate).
This situation is rather interesting because the C++ standard dictates
that only one user-defined implicit conversion can be done in an
expression. To translate (a) _b; into a::a(_b.operator a()) or
a::a(_b.operator int()) seems to only need one implicit conversion, so
the code should not work on a standards-conforming compiler.
I bet the code will compile if you remove either the overloaded
constructor a::a(int), or either operator in b. You could also try
making a::a(const a&) private.
jean-frangois
_______________________________________________
xcode-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/xcode-users
Do not post admin requests to the list. They will be ignored.
_______________________________________________
xcode-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/xcode-users
Do not post admin requests to the list. They will be ignored.