I have written the following code:
struct Element
{
int value;
};
struct Array
{
operator Element*();
operator const Element*() const;
Element& operator[](const size_t nIndex);
const Element& operator[](const size_t nIndex) const;
};
int main()
{
Array values;
if (values[0].value == 10)
{
}
return 0;
}
This works fine in x64. But in x86 I get a compiler error:
error C2666: 'Array::operator []': 4 overloads have similar conversions note: could be 'const Element &Array::operator [](const std::size_t) const' note: or 'Element &Array::operator [](const std::size_t)' note: while trying to match the argument list '(Array, int)' error C2228: left of '.value' must have class/struct/union
If I comment out the implicit conversions func, or add the explicit prefix, the code is compilable in x86.
But I can't understand why this code makes trouble.
Why can't the compiler decide to use an implicit conversion first, or array accessor first? I thought operator[] is higher in precedence.