a pointer to a reference

Discussion in 'C++' started by asdf, Sep 15, 2006.

  1. asdf

    asdf Guest

    C++ allows a reference to a pointer, but doesn't allow a pointer to a
    reference, why?
     
    asdf, Sep 15, 2006
    #1
    1. Advertising

  2. asdf

    Noah Roberts Guest

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


    What would be its purpose?
     
    Noah Roberts, Sep 15, 2006
    #2
    1. Advertising

  3. 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
     
    Thomas Tutone, Sep 15, 2006
    #3
  4. asdf

    Marcus Kwok Guest

    Thomas Tutone <> wrote:
    > 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.


    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.

    > but that still contradicts your
    > premise.


    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Sep 15, 2006
    #4
  5. asdf

    Marcus Kwok Guest

    Thomas Tutone <> wrote:
    > 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.


    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
    }

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Sep 15, 2006
    #5
  6. Marcus Kwok wrote:
    > Thomas Tutone <> wrote:
    > > 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.

    >
    > 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
     
    Thomas Tutone, Sep 15, 2006
    #6
  7. asdf

    Kaz Kylheku Guest

    asdf wrote:
    > 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.
     
    Kaz Kylheku, Sep 15, 2006
    #7
  8. asdf wrote:
    > 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.
     
    Gianni Mariani, Sep 15, 2006
    #8
  9. asdf

    Jim Langston Guest

    "asdf" <> wrote in message
    news:...
    > 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.
     
    Jim Langston, Sep 15, 2006
    #9
  10. asdf

    Kaz Kylheku Guest

    Marcus Kwok wrote:
    > 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.

    >
    > > Now a reference is simply an alias
    > > for another variable, so p_ref==p,

    >
    > 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.
     
    Kaz Kylheku, Sep 15, 2006
    #10
  11. asdf

    Kaz Kylheku Guest

    Gianni Mariani wrote:
    > asdf wrote:
    > > 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.


    .... 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!
     
    Kaz Kylheku, Sep 15, 2006
    #11
  12. asdf

    Marcus Kwok Guest

    Kaz Kylheku <> wrote:
    > Marcus Kwok wrote:
    >> 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.


    I contend that a reference can't exist without something to refer to.

    >> > Now a reference is simply an alias
    >> > for another variable, so p_ref==p,

    >>
    >> This I agree with.

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

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Sep 18, 2006
    #12
    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. sam pal
    Replies:
    3
    Views:
    566
    E. Robert Tisdale
    Jul 16, 2003
  2. Replies:
    10
    Views:
    717
    Chris Torek
    Feb 4, 2005
  3. Replies:
    4
    Views:
    405
    Andre Kostur
    Dec 24, 2007
  4. mathieu
    Replies:
    8
    Views:
    518
    Juha Nieminen
    Aug 31, 2008
  5. A
    Replies:
    7
    Views:
    644
Loading...

Share This Page