Topic: op[](size_t), op X*(), and integer constants


Author: "Brad Daniels" <brad.daniels@missioncritical.com>
Date: 1997/11/22
Raw View
MSVC++ 5.0 claims the following is ambiguous:

class AAA {
  public:
   operator const char *() const;
   const char operator[](size_t) const;
   void foo();
};
void f(AAA &x)
{
   if (x[1] == 'x')
    x.foo();
}

Apparently, it can't decide whether to convert x to const char *, then use
the regular [] operator, or to use the [] operator provided by the class.
If the argument to x[] is a size_t, it works.  This behavior looks like a
bug to me, but I'm not entirely sure, since the precedence rules for
conversions can get confusing.  Actually, I'm not even sure that the
compiler is allowed to use a user-defined conversion before applying the
built-in [] operator.  It certainly seems counter to the way user-defined
operators work...

Assuming the conversion is even a legal way to go, it seems to me that since
op[] requires only standard conversion sequences and operator const char*()
const requires a user-defined conversion sequence, operator[] should be
preferred.  Please e-mail me any responses in addition to posting, since
I've had some trouble with propagation in this newsgroup.

- Brad
--
----------------------------------------------------------------------------
Brad Daniels                  |  Was mich nicht umbringt macht mich hungrig.
Mission Critical Software     |   - Otto Nietzche
Standard disclaimers apply    |     (Friedrich's corpulent brother)
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Matt Austern <austern@isolde.mti.sgi.com>
Date: 1997/11/22
Raw View
"Brad Daniels" <brad.daniels@missioncritical.com> writes:

> MSVC++ 5.0 claims the following is ambiguous:
>
> class AAA {
>   public:
>    operator const char *() const;
>    const char operator[](size_t) const;
>    void foo();
> };
> void f(AAA &x)
> {
>    if (x[1] == 'x')
>     x.foo();
> }
>
> Apparently, it can't decide whether to convert x to const char *, then use
> the regular [] operator, or to use the [] operator provided by the class.
> If the argument to x[] is a size_t, it works.  This behavior looks like a
> bug to me, but I'm not entirely sure, since the precedence rules for
> conversions can get confusing.  Actually, I'm not even sure that the
> compiler is allowed to use a user-defined conversion before applying the
> built-in [] operator.  It certainly seems counter to the way user-defined
> operators work...

The compiler is right, the code really is ambiguous.  It can either
convert the AAA to a const char* and then apply the built-in indexing
operation, or else it can convert the int to a size_t and then use
the user-provide operator[].  Neither one is preferred.

I've seen this problem a several times, by the way---and every time
I've ever seen it, it's in a string class that provides an automatic
conversion to char*.  That's one reason why class basic_string<> in
the FDIS does not provide such an automatic conversion.
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]