Re: How to subscript a CString? (CSimpleStringT::operator)
On Fri, 9 Mar 2007 09:13:14 +0900, "Norman Diamond"
"Tom Walker" <firstname.lastname@example.org> wrote in message
CString s = _T("ab");
short i = 1;
_TCHAR c = s[i];
Error C2666, ambiguous overload.
The compiler sees an ambiguity between these two possibilities:
_TCHAR c = s.operator(i);
_TCHAR c = s.operator LPCTSTR()[i];
Thank you, though i am short of complete understanding. If i is changed to
an int then it works. So disambiguation occurs before the integral
promotion, and disambiguation fails because the conversion from short to int
ranks equally with the user-defined cast operator? ("user-defined" from the
standard's point of view because operator LPCTSTR comes from MFC not from
I believe this fragment models your example:
void f(X& x)
int y = x[short(1)];
VC8 emits the error:
X>cl -c a.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for
Copyright (C) Microsoft Corporation. All rights reserved.
a.cpp(9) : error C2666: 'X::operator ' : 2 overloads have similar
a.cpp(3): could be 'int X::operator (int)'
or 'built-in C++ operator[(int *, short)'
while trying to match the argument list '(X, short)'
This is a bug. Both candidate functions require promotion of short to int,
but the operator is an exact match for the implicit object parameter,
while the conversion operator requires conversion of it to int*, which is
worse. (Reading the error message literally, it's like the compiler ignores
the user-defined conversion that occurs when it applies operator*, and to
make matters worse, ignores the fact that short is promoted to int, so
there cannot be any such "signature" for the built-in operator.) Because
operator provides a better match on one of the arguments and isn't worse
on the other, it should be selected. You might want to report this here:
Post back with the URL, and I'll verify it.
Visual C++ MVP