8

Consider the following code:

struct X
{
    int x;
};

X xInstance;

class A
{
public:
    operator X*()
    {
        return &xInstance;
    }
};

int main()
{
    A a;
    *a = X();   // ok
    a[0] = X(); // ok
//  a->x = 0;   // error
}

A has an implicit conversion to a pointer type. I attempt to use it in three contexts in which a pointer is required; the two first lines are fine, but attempting to reference a field of struct X through operator-> relying on the implicit conversion to X* does not work. Why is that? Conceptually, how is operator[] different from operator-> in this context?

Tested with g++ 6.3.0 and VC++ 2017.

0

1 Answer 1

6

Standard section 13.6 lists the "candidate operator functions" that represent the built-in operators for purposes of overload resolution. When at least one subexpression of an operator @ has class or enum type, the list of functions considered for overload resolution is the union of the non-member lookup of operator@, the member lookup of operator@, and these candidate operator functions.

For most operators, the candidate operator functions are general enough to represent all the types permitted by the built-in operator. For example,

For every cv-qualified or cv-unqualified object type T, there exist candidate operator functions of the form

T& operator*(T*);

For every cv-qualified or cv-unqualified object type T there exist candidate operator functions of the form

T& operator[](T*, std::ptrdiff_t);
T& operator[](std::ptrdiff_t, T*);

When you write *a or a[0], the corresponding candidate operator function wins overload resolution, the subexpressions are converted to the argument types of the candidate operator function, and then the ordinary built-in operator rules apply.

However, the section does not list any candidate operator functions for operator->. So if a has class type, the only possible function for a->x is the member lookup of a.operator->(). (Non-member lookup does not apply to operator->, which must always be a member function.)

5
  • C++14 [over.match.oper]/3.3 also specifically says that for operator-> (and operator&) the built-in candidates set is empty
    – M.M
    Commented May 1, 2017 at 23:59
  • @M.M Good catch. I thought that was in there somewhere.
    – aschepler
    Commented May 2, 2017 at 0:00
  • @aschepler @M.M But what about [over.match.oper]/9? With no viable function (as there are no candidates to begin with), why isn't the built-in operator -> considered?
    – TSmith
    Commented May 3, 2022 at 19:08
  • @TSmith It is the built-in operator for that reason, yes. And [expr.ref]/2 says for an arrow operator "the first expression shall be a prvalue having pointer to complete class type". So it's an ill-formed use of the built-in operator.
    – aschepler
    Commented May 6, 2022 at 1:51
  • @aschepler Ah, I think I was thrown off by the notes (particularly #2 and #3) in [conv.general]. I assumed that implicit conversions always automatically applied wherever they could be used to meet destination type requirements (hence the "implicit"), but I see now that this is only true for standard conversions. (Another example of why these notes should be taken with a grain of salt!)
    – TSmith
    Commented May 6, 2022 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.