C++ Logo

std-proposals

Advanced search

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

From: Sean Mayard <seanmayard_at_[hidden]>
Date: Tue, 9 May 2023 17:40:35 +0530
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.

Regards,
Sean(ASR)

Received on 2023-05-09 12:10:48