a pointer to a reference

T

Thomas Tutone

asdf wrote:

C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?

You're mistaken:

int main()
{
int i;
int& ref_i = i; // reference to an int
int* p = &i; // pointer to an int
int* p_ref = &ref_i; // pointer to a reference to an int.
}

p_ref is a pointer to a reference. Now a reference is simply an alias
for another variable, so p_ref==p, but that still contradicts your
premise.

Best regards,

Tom
 
M

Marcus Kwok

Thomas Tutone said:
You're mistaken:

int main()
{
int i;
int& ref_i = i; // reference to an int
int* p = &i; // pointer to an int
int* p_ref = &ref_i; // pointer to a reference to an int.
}

p_ref is a pointer to a reference.

I tend to disagree. Once a reference has been seated, any use of it
really refers to the referenced object, so (p_ref = &ref_i) really means
(p_ref = &i), so it is a pointer to the referenced object, but not
really a pointer to the actual reference. Though, this may be splitting
hairs, and I can see how your interpretation can be seen as correct too,
since a reference doesn't really exist on its own.
Now a reference is simply an alias
for another variable, so p_ref==p,

This I agree with.
 
M

Marcus Kwok

Thomas Tutone said:
You're mistaken:

int main()
{
int i;
int& ref_i = i; // reference to an int
int* p = &i; // pointer to an int
int* p_ref = &ref_i; // pointer to a reference to an int.
}

p_ref is a pointer to a reference. Now a reference is simply an alias
for another variable, so p_ref==p, but that still contradicts your
premise.

As another followup, taken literally it is illegal:

int main()
{
int i = 42;
int& r = i;
int&* p = &r; // illegal: pointer to reference is not allowed
}
 
T

Thomas Tutone

Marcus said:
As another followup, taken literally it is illegal:

int main()
{
int i = 42;
int& r = i;
int&* p = &r; // illegal: pointer to reference is not allowed
}

Good point. I hadn't thought of it that way.

In any case, I guess the reason is that how a reference is implemented
is, by definition, an implementation detail. In some instances, a
reference might be implemented under the hood as as a const pointer, in
other instances it may be a true alias with no separate storage. In
both cases, but particularly the latter, a pointer to the alias, as
opposed to the aliased object, wouldn't make much sense.

Best regards,

Tom
 
K

Kaz Kylheku

asdf said:
C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?

Of course it does. A pointer to a reference is a pointer to the target
object.

int i = 3;
int &ri = i;
int *pi = &ri; // pointer to reference

But, of course, this isn't being done with a special
"pointer-to-reference" type, but a regular pointer.

This is consistent with the "invisible" semantics of a reference.

There would be no utility in pointing to a reference. Dereferencing
that pointer would cause the resulting lvalue to "slide" to the target
object.

Since a reference cannot be reseated, you would not be gaining any
additional level of indirection by doing this.

What's worse, you could create new modes of failure. The reference can
potentially have a shorter lifetime than the object it points to,
meaning that the reference could be destroyed, thus invalidating the
pointer-to-reference even though the target object still exists.

Lastly, there is no syntax for obtaining the address of a reference, so
you'd have no way to initialize such a pointer. The address of a
reference yields the address of the referent object. So
pointers-to-reference would have to be accompanied by special syntax.
 
G

Gianni Mariani

asdf said:
C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?

References are special beasts in that they may or may not take storage.
From an implementation perspective, it's impossible to point to
something that takes no storage.

The best you can do is create a struct that contains a reference (and
hence storage) and point to it.
 
J

Jim Langston

asdf said:
C++ allows a reference to a pointer, but doesn't allow a pointer to a
reference, why?

Because a reference really doesn't exist as an object per se.

In reality, most compilers probably implement a reference as a pointer and
treat it differently, but they don't have to. It could be some entry in a
table, or anything else a compiler designer wanted to implement, AFAIK.

And there is the rule that a reference can't be reseated.

As such, a pointer to a reference really gives you nothing. What would it
actually point to? The reference may no actually be stored in memory
anywhere (although it most likely is). What would you do with this pointer
to a reference? You can't change the reference itself (they're not allowed
to be reseated).

References don't follow the same types of rules as pointers, such as you
can't store them in an array, etc.. (AFAIK, I may be wrong on this, but I
think it's right).

If you need to get a pointer to a reference, then most likely you don't want
a referance in the first place but a pure pointer. Pointers to pointers are
allowed.
 
K

Kaz Kylheku

Marcus said:
I tend to disagree. Once a reference has been seated, any use of it
really refers to the referenced object, so (p_ref = &ref_i) really means
(p_ref = &i), so it is a pointer to the referenced object, but not
really a pointer to the actual reference. Though, this may be splitting
hairs, and I can see how your interpretation can be seen as correct too,
since a reference doesn't really exist on its own.

Of course a reference exists on its own.
This I agree with.

That is only an optimization. In the general case, references are real
run-time entities. They just aren't integrated into the type system as
first-class objects, that's all. Many situations require references to
occupy memory locations.

In these examples, references must correspond to something in the
run-time:

extern int &return_reference(void);

If a reference isn't real, how does the above function return
something? If it was inlined, then the reference could disappear, but
how can that happen under an external call? The function can return the
location of some arbitrary object. That location can be captured by the
caller, who can then modify that object. The function can choose a
different object each time you call it; and the object can even be
dynamically allocated.

How about:

struct s {
int &r;
};

If a reference isn't real, shouldn't sizeof(s) be 1? How will the
program retrieve the member r from an arbitrary struct s? There could
be millions of dynamically allocated instances of struct s, all with
different references r.

The compiler cannot account for these with a compile-time alias trick.
 
K

Kaz Kylheku

Gianni said:
References are special beasts in that they may or may not take storage.

.... an attribute which they share with constants and lexical variables.
From an implementation perspective, it's impossible to point to
something that takes no storage.

That's completely irrelevant, since you can point to constants and
variables. When you take their address, the language implementation
ensures that something exists that allows the pointer to be
instantiated. I.e. it has to defeat certain optimizations from taking
place.

The same could be arranged for a pointer-to-reference mechanism.

The real reason why there isn't one is that it would be close to
useless, and supporting it would require additional syntax and
semantics to be present in references, which would basically reduce
them to having some of the functionality of pointers which they
deliberately do not have.
The best you can do is create a struct that contains a reference (and
hence storage) and point to it.

Wait a minute, didn't you say that references may not take storage? Doh!
 
M

Marcus Kwok

Kaz Kylheku said:
Of course a reference exists on its own.

I contend that a reference can't exist without something to refer to.
That is only an optimization.

I was just confirming the actions of the (snipped) code.
In the general case, references are real
run-time entities. They just aren't integrated into the type system as
first-class objects, that's all.
Yes.

Many situations require references to
occupy memory locations.

Right, but AFAIK the Standard does not.
In these examples, references must correspond to something in the
run-time:

extern int &return_reference(void);

If a reference isn't real, how does the above function return
something? If it was inlined, then the reference could disappear, but
how can that happen under an external call? The function can return the
location of some arbitrary object. That location can be captured by the
caller, who can then modify that object. The function can choose a
different object each time you call it; and the object can even be
dynamically allocated.

OK, but in each case, you have the reference and the thing it refers to.
The objects can exist without the references, but the references cannot
exist without the objects.
How about:

struct s {
int &r;
};

If a reference isn't real, shouldn't sizeof(s) be 1? How will the
program retrieve the member r from an arbitrary struct s? There could
be millions of dynamically allocated instances of struct s, all with
different references r.

The compiler cannot account for these with a compile-time alias trick.

OK, but I would consider this an implementation detail.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top