rvalues and constant reference to a pointer ?

A

amparikh

I know this is something fundamental and I ought to have known it, but
somehow this seems to be confusing me a lot.

Fundamentally, rvalues and/or temporaries can be bound only to constant
references going by the const guidelines.

Taking that into consideration, how does one get a constant reference
to a pointer.

class A
{};

1> A* foo(A* & ptr){ } // non-const reference to a pointer

2> A* foo( const A* & ptr) { } //reference to a pointer to const A

3> A* foo(A* const & ptr) { } // reference to a constant pointer to A

Now when I make a call like

foo( new A()); or
foo( &A());

The first one doesnt work as it is a non const reference and neither
does the second which is a non const reference to a pointer to const
object, which makes sense.

It does work with the third declaration which again is "ONLY a
reference to a const pointer"...( and this is what I have been using
all along when I have to do something like this).

Now my question is, logically speaking rvalues/temporaries can be bound
only to const references...so the two calls that I have to foo,
shouldnt work with the 3 declaration...but it does work, which I have
just blindly assumed until now and used it whenever I run into such
situations.

But then how does one realy have a constant reference to a pointer in
this case ? const usually binds to the left(unless there is nothing to
the left, so then it binds to the right)...so in that case it would be
the same as declaration no 3.

which would mean that in reality, there is nothing as a const reference
to a pointer.

Thanks.
 
M

Me

1> A* foo(A* & ptr){ } // non-const reference to a pointer
2> A* foo( const A* & ptr) { } //reference to a pointer to const A
3> A* foo(A* const & ptr) { } // reference to a constant pointer to A

Now when I make a call like

foo( new A()); or
foo( &A());

The first one doesnt work as it is a non const reference and neither
does the second which is a non const reference to a pointer to const
object, which makes sense.

It does work with the third declaration which again is "ONLY a
reference to a const pointer"...( and this is what I have been using
all along when I have to do something like this).

Now my question is, logically speaking rvalues/temporaries can be bound
only to const references...so the two calls that I have to foo,
shouldnt work with the 3 declaration...but it does work, which I have
just blindly assumed until now and used it whenever I run into such
situations.

Rewrite your examples using typedefs:

typedef A * Ap;
typedef const A * C_Ap;

1> Ap foo(Ap &ptr);
2> Ap foo(C_Ap &ptr);
3> Ap foo(const Ap &ptr);

Still confused?

4> Ap foo(const C_Ap &ptr);

also works. Get yourself a copy of the standard and read section 4.4
about const safety to see why (it's hard to see why here, but if you
write it out without the typedefs, it's easier).
 
O

Old Wolf

Fundamentally, rvalues and/or temporaries can be bound only to constant
references going by the const guidelines.

Taking that into consideration, how does one get a constant reference
to a pointer.

3> A* foo(A* const & ptr) { } // reference to a constant pointer to A

Actually this is a const reference to a pointer to A.
Since the reference is const, it's irrelevant whether or not
the pointer it's referring to is const (because the reference
cannot be used to modify it, in either case).

This explains why your code samples work with this version,
and makes the rest of your message moot.
 
M

Me

3> A* foo(A* const & ptr) { } // reference to a constant pointer to A
Actually this is a const reference to a pointer to A.
Since the reference is const, it's irrelevant whether or not
the pointer it's referring to is const (because the reference
cannot be used to modify it, in either case).

His comment is right. A * & const would be a const reference to a
pointer to A (which is meaningless:
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.7).
 
R

Rolf Magnus

Old said:
Actually this is a const reference to a pointer to A.

There is no such thing as a "const reference". The above is a reference to
"A* const", just like "A* const *" would be a (non-const) pointer to
"A* const".
 
O

Old Wolf

Rolf said:
There is no such thing as a "const reference".

A 'const reference' is a reference where the thing it's
referring to, can't be modified via the reference.
(I don't know if this term is in the Standard, but it seems
fairly common practice for describing a type such as T const & )
The above is a reference to "A* const"

In this case:

A *p;
A * const &q = p;

q refers to p, and p is an A * (not an A * const).
However, p looks like an A * const when viewed through q.

The OP did ask, "how does one get a constant reference
to a pointer". (to which the answer is, his #3).
 
R

Rolf Magnus

Old said:
A 'const reference' is a reference where the thing it's
referring to, can't be modified via the reference.
(I don't know if this term is in the Standard, but it seems
fairly common practice for describing a type such as T const & )

I use it myself sometimes, because it's a bit shorter than "reference to
const".
In this case:

A *p;
A * const &q = p;

q refers to p, and p is an A * (not an A * const).
However, p looks like an A * const when viewed through q.

And what about this case:

A *p;
A * const * q = &p;

p looks like an A * const when viewed through the dereferenced q. Still I
wouldn't call p a const pointer, since it isn't const. It's a pointer to
const, even though the pointed-to object is actually not a constant.
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top