Thread View: comp.std.c++
2 messages
2 total messages
Started by Jody Goldberg
Tue, 21 Nov 1995 04:26
Is this ambiguous ?
Author: Jody Goldberg
Date: Tue, 21 Nov 1995 04:26
Date: Tue, 21 Nov 1995 04:26
22 lines
1091 bytes
1091 bytes
Is this ambiguous ?
struct A
{
operator int *();
operator int const *();
};
void f()
{
A a;
int i = a[0];
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
jody+@cmu.edu
[ 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. ]
Re: Is this ambiguous ?
Author: Steve Clamage
Date: Tue, 21 Nov 1995 16:38
Date: Tue, 21 Nov 1995 16:38
34 lines
1582 bytes
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. ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads