I'm reading C++ Templates - The Complete Guide, 2nd Edition, and B.2.1 tells about implicit conversion of the implied "this" argument.
Same example here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1592.pdf
Depending on the typedef of ptrdiff_t, the compiler may deduce that there is an ambiguity between BadString::operator[] and converting the implied "this" argument to char * and using the built-in subscript operator.
Can somebody please explain how is obj[0] expression related to this conversion and why compiler acts the way it acts in three examples below?
Thank you.
int main() {
abc x;
auto first = x[1];
auto second = x + 2;
return 0;
}
Works (why?):
struct abc
{
operator bool *() { return {}; }
};
Doesn't work (why):
struct abc
{
template <typename T>
operator T *() = delete;
};
template <>
abc::operator int *() { return {}; }
Doesn't work (use of overloaded operator '[]' is ambiguous):
struct abc
{
operator bool *() { return {}; }
template <typename T>
operator T *() = delete;
};
template <>
abc::operator int *() { return {}; }