Boost logo

Boost :

From: Beman Dawes (bdawes_at_[hidden])
Date: 2003-02-02 20:02:11


At 04:18 PM 2/2/2003, David Abrahams wrote:

>> In other words, there are four interface combinations:
>>
>> When T is not an array && conversion to T* is not desired:
>>
>> T& operator*() const;
>> T* operator->() const;
>>
>> When T is not an array && conversion to T* is desired:
>>
>> T& operator*() const;
>> T* operator->() const;
>> operator T*() const;
>>
>> When T is an array && conversion to T* is not desired:
>>
>> T& operator[](size_t i) const;
>>
>> When T is an array && conversion to T* is desired:
>>
>> operator T*() const;
>
>Why can't operator T*() and operator[](size_t) coexist?

They are ambiguous, or at least GCC, Intel, and Microsoft compilers think
so, although Borland only warns and Metrowerks accepts.

The Intel error message says:

test.cpp(15): error: more than one operator "[]" matches these operands:
built-in operator "pointer-to-object[integer]" function
"foo<T>::operator[](unsigned int) [with T=int]" operand types are: foo<int>
[ int ]

That being said, if size_t is changed to int, all of the above compilers
accept the code and select the correct overload. However, both VC++ 6.0 and
7.0 fail if the argument to [] happens to be size_t.

The only way to get it to work with both VC++ 6.0 and 7.0 is to remove the
operator T*().

So, yes, you can get operator[] and operatorT* to co-exist, but you have to
be very careful with parameter types. Plus you can't use operator[] and
operatorT* together with VC++ 6.0 or 7.0. Hopefully that is fixed in 7.1,
but I haven't tried it.

--Beman


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk