Topic: overload resolution with operator[] ?


Author: "Carl Daniel" <carl@pixami.com>
Date: Tue, 25 Sep 2001 18:23:26 GMT
Raw View
"Paul Mensonides" <leavings@home.com> wrote in message
news:piRr7.34913$QK.26971481@news1.sttln1.wa.home.com...
> If I have this class:
>
> -------------------------
>
> struct A {
>     int data[5];
>     int& operator[](unsigned i);
>     operator int*(void);
> };
>
> -------------------------
>
> Is this syntax ambiguous?
>
> -------------------------
>
> A a = { 1, 2, 3, 4, 5 };
> a[0] = 10; // is this ambiguous?
>
> -------------------------
>
> Comeau C++ gives an error, but aren't the "usual arithmetic conversions" a
> better match than user-defined conversion operators?
>
> a.operator int*()[0];
>
>     -vs.-
>
> a.operator[](unsigned(0));
>

Every compiler I've tried that on has called it ambiguous.

According to 5.2.1, a[0] is equivalent to *(a+0).  Since a provides a
conversion to pointer, this is *(a.operator int*()+0).

According to 13.3.1.2, a[0] is also equivalent to a.operator[](0).

According to 13.3.2, these are both viable functions (if 13.3.2 even
applies...)

That's about as far as I can reason it out.  Function overload resolution
doesn't seem to be enough to handle this case, since the (potential)
ambiguity is between two very different sequences: the first, (implicit)
conversion of an object of class type to a pointer, followed by normal
pointer de-referencing.  The second, the re-writing of a subscript
expression to the call of a member function on an object of class type with
a parameter which is an exact match.

Personally, I'd prefer it if a[0] called operator[](), but I'm not sure
what's right :)  Of course, having an operator int*() is evil incarnate, but
you knew that already...

-cd



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Paul Mensonides" <leavings@home.com>
Date: Wed, 26 Sep 2001 22:52:54 GMT
Raw View
"Carl Daniel" <carl@pixami.com> wrote in message
news:9oqgth$hs0@dispatch.concentric.net...
> Personally, I'd prefer it if a[0] called operator[](), but I'm not sure
> what's right :)  Of course, having an operator int*() is evil incarnate,
but
> you knew that already...

How about this instead?

struct A {
    int data[5];
    int& operator[](unsigned i);
    template<class T> operator T(void) {
        return *reinterpret_cast<T*>(this);
    }
};

Paul Mensonides :)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Paul Mensonides" <leavings@home.com>
Date: Tue, 25 Sep 2001 15:34:14 GMT
Raw View
If I have this class:

-------------------------

struct A {
    int data[5];
    int& operator[](unsigned i);
    operator int*(void);
};

-------------------------

Is this syntax ambiguous?

-------------------------

A a = { 1, 2, 3, 4, 5 };
a[0] = 10; // is this ambiguous?

-------------------------

Comeau C++ gives an error, but aren't the "usual arithmetic conversions" a
better match than user-defined conversion operators?

a.operator int*()[0];

    -vs.-

a.operator[](unsigned(0));

Paul Mensonides

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]