C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Why [expr.ref] says that E1->E2 is converted to (*(E1)).E2

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Tue, 9 May 2023 14:05:05 +0100
On Tue, 9 May 2023 at 13:10, Sean Mayard via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> I read [expr.ref#2] which includes the following sentence:
>
> > *The expression E1->E2 is converted to the equivalent form (*(E1)).E2*
>
> My question is, does this mean that "every" expression that has the form
> *E1->E2* will be converted to *(*(E1)).E2. *I mean it is not clear to me
> in what situations/contexts does the above sentence hold?
>
> For example, suppose we have a class that overloads *operator* *but does
> not overload *operator->* then if we use *operator-> *with an object of
> that class type, then that expression will be first converted to
> *(*(E1)).E2* form?
>
> #include <iostream> struct A { int foo = 1, bar =2;};struct B { A a{}; A& operator* () { return a; }}; int main(){ B b{}; int x = b->foo; //i know this will give error because there is no overloaded operator-> //but according to [expr.ref#2] shouldn't b->foo be first converted to (*(b)).foo //and then the overloaded operator* should be used }
>
>
> As you can see in the above program, `b->foo;` gives an error because the
> class `B` has not overloaded *operator-> *. But according to [expr.ref#2]
> the expression *b->foo *should be converted to *(*(b)).foo *and then the *operator*
> *could be used.
>
> So I want to know what exactly does [expr.ref#2] mean when it said " *The
> expression E1->E2 is converted to the equivalent form (*(E1)).E2* ". Does
> this conversion supposed to happen only after `E1->E2` is valid or before.
> Is a CWG issue required for this or I just did not read/interpret it
> correctly.
>

See [expr.pre] which explains that the rules in Clause [expr] apply to the
built-in operators, not to overloaded operators.

"Subclause 7.6 defines the effects of operators when applied to types for
which they have not been overloaded." (and also the note before that).

Received on 2023-05-09 13:05:20