Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   rvalues and constant reference to a pointer ? (http://www.velocityreviews.com/forums/t291590-rvalues-and-constant-reference-to-a-pointer.html)

amparikh@gmail.com 06-08-2005 12:48 AM

rvalues and constant reference to a pointer ?
 
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.


Me 06-08-2005 02:05 AM

Re: rvalues and constant reference to a pointer ?
 
> 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).


Old Wolf 06-08-2005 04:34 AM

Re: rvalues and constant reference to a pointer ?
 
amparikh@gmail.com wrote:
>
> 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.


Me 06-08-2005 05:52 AM

Re: rvalues and 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).


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-lit...html#faq-18.7).


Rolf Magnus 06-08-2005 10:37 AM

Re: rvalues and constant reference to a pointer ?
 
Old Wolf wrote:

> amparikh@gmail.com wrote:
>>
>> 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.


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".


Old Wolf 06-08-2005 12:51 PM

Re: rvalues and constant reference to a pointer ?
 
Rolf Magnus wrote:
> Old Wolf wrote:
>
>> amparikh@gmail.com wrote:
>>>
>>> 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.

>
> 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).


Rolf Magnus 06-08-2005 03:19 PM

Re: rvalues and constant reference to a pointer ?
 
Old Wolf wrote:

> Rolf Magnus wrote:
>> Old Wolf wrote:
>>
>>> amparikh@gmail.com wrote:
>>>>
>>>> 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.

>>
>> 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 & )


I use it myself sometimes, because it's a bit shorter than "reference to
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.


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.



All times are GMT. The time now is 09:05 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.