rvalues and constant reference to a pointer ?

Discussion in 'C++' started by amparikh@gmail.com, Jun 8, 2005.

  1. Guest

    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.
     
    , Jun 8, 2005
    #1
    1. Advertising

  2. Me Guest

    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).
     
    Me, Jun 8, 2005
    #2
    1. Advertising

  3. Old Wolf Guest

    Re: rvalues and constant reference to a pointer ?

    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.
     
    Old Wolf, Jun 8, 2005
    #3
  4. Me Guest

    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-lite/const-correctness.html#faq-18.7).
     
    Me, Jun 8, 2005
    #4
  5. Rolf Magnus Guest

    Re: rvalues and constant reference to a pointer ?

    Old Wolf wrote:

    > 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".
     
    Rolf Magnus, Jun 8, 2005
    #5
  6. Old Wolf Guest

    Re: rvalues and constant reference to a pointer ?

    Rolf Magnus wrote:
    > Old Wolf wrote:
    >
    >> 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).
     
    Old Wolf, Jun 8, 2005
    #6
  7. Rolf Magnus Guest

    Re: rvalues and constant reference to a pointer ?

    Old Wolf wrote:

    > Rolf Magnus wrote:
    >> Old Wolf wrote:
    >>
    >>> 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.
     
    Rolf Magnus, Jun 8, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven T. Hatton
    Replies:
    11
    Views:
    682
    Alf P. Steinbach
    Apr 20, 2004
  2. Lvalues and Rvalues

    , Oct 14, 2006, in forum: C Programming
    Replies:
    3
    Views:
    357
    SM Ryan
    Oct 14, 2006
  3. Steven T. Hatton

    cv-qualified rvalues

    Steven T. Hatton, Dec 14, 2006, in forum: C++
    Replies:
    1
    Views:
    821
    Victor Bazarov
    Dec 14, 2006
  4. Nicklas Karlsson

    lvalues and rvalues

    Nicklas Karlsson, Apr 6, 2010, in forum: C Programming
    Replies:
    127
    Views:
    2,637
    Tim Rentsch
    May 5, 2010
  5. G G
    Replies:
    3
    Views:
    96
    Ben Bacarisse
    Apr 20, 2014
Loading...

Share This Page