🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2696

Re: Is this ambiguous ?

#2696
From: Steve Clamage
Date: Tue, 21 Nov 1995 16:38
34 lines
1582 bytes
In article VAA26445@andrew.cmu.edu, Jody Goldberg <jody+@cmu.edu> writes:
>Is this ambiguous ?
>
>struct A
>{
>    operator int *();
>    operator int const *();
>};
>
>void f()
>{
>    A a;
>    int i = a[0];
>}

I believe it is ambiguous. The right side of the assignment requires
an int rvalue. Dereferencing the result of either conversion function
yields an int rvalue, so I don't see a way to choose between them.

You could use an explicit cast to choose one or the other:
    int j = ((const int*)a)[0];
    int k = ((int*)a)[0];

Type conversion operators in general tend to lead to ambiguities, and
often cause more problems than they solve. Having more than one in the
same class is much more likely to create ambiguities.
---
Steve Clamage, stephen.clamage@eng.sun.com



[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <48suoe$4hj@engnews1.eng.sun.com>
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
References: <199511210226.VAA26445@andrew.cmu.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
From: clamage@Eng.sun.com (Steve Clamage)
Newsgroups: comp.std.c++
Subject: Re: Is this ambiguous ?
Date: 21 Nov 1995 16:38:26 GMT
Organization: Sun Microsystems Inc.
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <48suoe$4hj@engnews1.eng.sun.com>
References: <199511210226.VAA26445@andrew.cmu.edu>
Reply-To: clamage@Eng
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
X-Nntp-Posting-Host: taumet.eng.sun.com
Content-Length: 783
X-Lines: 30
Originator: clamage@taumet