C++ Logo

std-discussion

Advanced search

Re: Class member access

From: Daniel Krügler <daniel.kruegler_at_[hidden]>
Date: Wed, 28 May 2025 13:47:28 +0200
Am Mi., 28. Mai 2025 um 13:05 Uhr schrieb Russell Shaw via Std-Discussion <
std-discussion_at_[hidden]>:

> Hi,
>
> In [expr.ref], there is:
>
> -----------------------------------------------
> If E2 is declared to have type “reference to T”, then E1.E2 is an lvalue
> of type
> T. If E2 is a static data member, E1.E2 designates the object or function
> to
> which the reference is bound, otherwise E1.E2 designates the object or
> function
> to which the corresponding reference member of E1 is bound. Otherwise, one
> of
> the following rules applies.
> -----------------------------------------------
>
> "E1.E2 designates the object or function to which the reference is bound"
> conflicts with "If E2 is a static data member"
>
> What does it mean ?
> One bit's saying it's an object and another saying it's a function.
>

There is no conflict. A data member can have type reference to function or
reference to object.

Consider:

int var;

using f_t = void();

void f(){}

struct C
{
  static int& sr;
  int& r;
  static f_t& sf;
} c(var);

int& C::sr = var;
f_t& C::sf = f;

int main()
{
  (void) c.sr;
  (void) c.r;
  (void) c.sf;
}


>
> Now take: "E1.E2 designates the object or function to which the reference
> is
> bound, otherwise E1.E2 designates the object or function to which the
> corresponding reference member of E1 is bound". Isn't that two sentences
> saying
> the same thing ?
>

No. The sentence makes sense if your include the omitted first part of the
sentence: " If E2 is a static data member, ..."

Now we have two arms describing different situations:

1) "If E2 is a static data member, E1.E2 designates the object or function
to which the reference is bound": This is relevant for the static data
members C::sr and C::sf in above example
2) " otherwise E1.E2 designates the object or function to which the
corresponding reference member of E1 is bound ": This is relevant for the
non-static data meber C::r in above example.

- Daniel

Received on 2025-05-28 11:47:45