11

Consider this code:

struct AA
{
    int& rr;
};

Is there a way to obtain pointer (or maybe reference) to AA::rr in order to obtain this?

AA* aa;
auto mm = &AA::rr; // error: cannot create pointer to reference member ‘AA::rr’
aa ->* mm;

Also in gcc-7.0.1 decltype(AA::mm) is just int&. Is this according to the standard? And does this make sense?


EDIT

Sorry guys, I formulated the question not quite well. No complaints to the fact that references are not objects or that there is no such thing as pointer to a reference. The goal is quite selfish. Given struct AA { int& rr; }; I just want to have a function like this:

template < typename Class, typename Member >
void test(Member Class::*) { }

that when calling test(&AA::rr) I want Class to be AA and Member to be int& or int. So I don't even need the pointer itself but its type that will allow to retrieve the class type and the member type.

Vahagn
  • 4,670
  • 9
  • 43
  • 72
  • 3
    References are not objects as far as the standard is concerned, therefore you cannot do things like getting their address or memory representation. I think it is just not possible. – nwp Feb 08 '17 at 17:13
  • @Dan That gives you a pointer to the object that `rr` references, not to `rr` itself which I believe was the goal. – nwp Feb 08 '17 at 17:14
  • 3
    @Dan `&AA::rr` wouldn't be nonsense if `rr` was an `int` or `int*`. You [can](http://stackoverflow.com/q/670734/3425536) have pointers to member variables. – Emil Laine Feb 08 '17 at 17:18
  • @M.M actually it doesn't as it just forbids `&AA::rr` when `rr` is a reference. – Vahagn Feb 09 '17 at 07:47

2 Answers2

11

How to obtain pointer to reference (member)?

You cannot obtain a pointers to (or references to, or arrays of) references. There is no such type as "pointer to reference" in C++. This is because references are not required to have storage, so there might not even be an address where the reference is stored.

When you apply addressof operator on a reference, what you get is the address of the object that is referred to.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Even more, I don't think it's strictly true that a member reference must have storage. Given a sufficiently lenient ABI, a sufficiently aggressive optimiser, and a "cooperating" constructor evaluable at compile time, the compiler might skip the reference member. – Angew is no longer proud of SO Feb 08 '17 at 17:20
  • @Angew fair enough. I had hard time imagining optimizing it away, but I take your word for it. – eerorika Feb 08 '17 at 17:22
4

Picture speaks a thousand words

picture

References doesn't really have a container in the memory, they serves more like an alias to the original variable, thus you cannot get pointer to reference because references doesn't have their own memory location.

However, you can get the address of reference, which is the variable it is referencing. In this example, if you cout &rx and &x, they are the same.

So probably you would want to get a pointer to the object it is referencing, rather than pointer to reference

Zhou Zhi Hua
  • 371
  • 1
  • 10
  • 1
    *"References doesn't really have a container in the memory"*. Well, each reference might or might not have. But since neither is guaranteed, usually the worst case must be assumed. Worst case for taking the address is that there is no address. Worst case for memory optimization is that storage is used. Nice picture :) – eerorika Feb 08 '17 at 18:06
  • @user2079303 well, I never know reference might have container too, definitely will do more research on that, thanks for pointing out that! – Zhou Zhi Hua Feb 08 '17 at 18:10
  • 3
    `int &x = x;` initializes the reference with itself (undefined behaviour) – M.M Feb 09 '17 at 07:42