vector of const references. Is it possible?

Discussion in 'C++' started by ragged_hippy, Mar 7, 2007.

  1. ragged_hippy

    ragged_hippy Guest

    Hi,

    I wanted to create a vector of const references. Something like this:

    vector<const x&> y;

    where x is a class name.

    Is this a valid statement? Will this work if I use a reference of 'y'
    somewhere else?

    PJ.
    ragged_hippy, Mar 7, 2007
    #1
    1. Advertising

  2. ragged_hippy

    Noah Roberts Guest

    ragged_hippy wrote:
    > Hi,
    >
    > I wanted to create a vector of const references. Something like this:
    >
    > vector<const x&> y;
    >
    > where x is a class name.
    >
    > Is this a valid statement? Will this work if I use a reference of 'y'
    > somewhere else?


    It is not portable. const& is not assignable and therefore not a valid
    container element.
    Noah Roberts, Mar 7, 2007
    #2
    1. Advertising

  3. ragged_hippy

    mlimber Guest

    On Mar 7, 12:28 pm, "ragged_hippy" <> wrote:
    > I wanted to create a vector of const references. Something like this:
    >
    > vector<const x&> y;
    >
    > where x is a class name.
    >
    > Is this a valid statement? Will this work if I use a reference of 'y'
    > somewhere else?


    Did you try it? Any compiler will instantly tell you whether pointers
    to references are allowed. (Hint: they're not, but vector requires
    pointers to its containees.)

    Cheers! --M
    mlimber, Mar 7, 2007
    #3
  4. ragged_hippy

    Mike Wahler Guest

    "ragged_hippy" <> wrote in message
    news:...
    > Hi,
    >
    > I wanted to create a vector of const references.


    Why?

    > Something like this:
    >
    > vector<const x&> y;
    >
    > where x is a class name.


    Not valid.

    >
    > Is this a valid statement?


    No.

    > Will this work if I use a reference of 'y'


    No.

    Standard library containers store objects. References
    are not objects. References are alternate names for
    existing objects.

    > somewhere else?


    No.

    You can, however, store pointers in your vector, since
    pointers are objects.

    Perhaps if you describe what problem you're trying to
    solve, we might be able to give advice.

    -Mike
    Mike Wahler, Mar 7, 2007
    #4
  5. ragged_hippy

    ragged_hippy Guest

    On Mar 7, 11:58 am, "Mike Wahler" <> wrote:
    > "ragged_hippy" <> wrote in message
    >
    > news:...
    >
    > > Hi,

    >
    > > I wanted to create a vector of const references.

    >
    > Why?
    >
    > > Something like this:

    >
    > > vector<const x&> y;

    >
    > > where x is a class name.

    >
    > Not valid.
    >
    >
    >
    > > Is this a valid statement?

    >
    > No.
    >
    > > Will this work if I use a reference of 'y'

    >
    > No.
    >
    > Standard library containers store objects. References
    > are not objects. References are alternate names for
    > existing objects.
    >
    > > somewhere else?

    >
    > No.
    >
    > You can, however, store pointers in your vector, since
    > pointers are objects.
    >
    > Perhaps if you describe what problem you're trying to
    > solve, we might be able to give advice.
    >
    > -Mike


    Okay....I am trying to not use extra memory if possible.

    I have this list of objects as a private member of a container. And I
    have a public method defined in this container, that will take a
    reference to vector buffer as a parameter and fill it up with objects
    that it contains. If the vector for buffer is a vector of the object,
    then all objects will be copied into this buffer. So, I thought may be
    a vector of references might be better. But yes I immediately got his
    huge set of compiler errors which I didn't expect.

    Does this make sense?

    -PJ.
    ragged_hippy, Mar 7, 2007
    #5
  6. ragged_hippy

    Jim Langston Guest

    "ragged_hippy" <> wrote in message
    news:...
    > On Mar 7, 11:58 am, "Mike Wahler" <> wrote:
    >> "ragged_hippy" <> wrote in message
    >>
    >> news:...
    >>
    >> > Hi,

    >>
    >> > I wanted to create a vector of const references.

    >>
    >> Why?
    >>
    >> > Something like this:

    >>
    >> > vector<const x&> y;

    >>
    >> > where x is a class name.

    >>
    >> Not valid.
    >>
    >>
    >>
    >> > Is this a valid statement?

    >>
    >> No.
    >>
    >> > Will this work if I use a reference of 'y'

    >>
    >> No.
    >>
    >> Standard library containers store objects. References
    >> are not objects. References are alternate names for
    >> existing objects.
    >>
    >> > somewhere else?

    >>
    >> No.
    >>
    >> You can, however, store pointers in your vector, since
    >> pointers are objects.
    >>
    >> Perhaps if you describe what problem you're trying to
    >> solve, we might be able to give advice.
    >>
    >> -Mike

    >
    > Okay....I am trying to not use extra memory if possible.
    >
    > I have this list of objects as a private member of a container. And I
    > have a public method defined in this container, that will take a
    > reference to vector buffer as a parameter and fill it up with objects
    > that it contains. If the vector for buffer is a vector of the object,
    > then all objects will be copied into this buffer. So, I thought may be
    > a vector of references might be better. But yes I immediately got his
    > huge set of compiler errors which I didn't expect.
    >
    > Does this make sense?
    >
    > -PJ.


    So just store them as a vector of pointers.

    std::vector<x*>
    Jim Langston, Mar 7, 2007
    #6
  7. ragged_hippy

    Puppet_Sock Guest

    On Mar 7, 1:09 pm, "ragged_hippy" <> wrote:
    [snip]
    > I have this list of objects as a private member of a container.

    [snip]

    Maybe instead of making some other container to hold this
    data, you can keep holding it where it is. Maybe you can
    add the functionality you want to the class that is already
    doing the work of holding the data.

    Alternatively, maybe the class that is doing the work of
    holding this data isn't the right spot for it. Maybe it
    should be held someplace else, and the work done to it
    over there as well.

    That is to say, possibly you have some refactoring to do.
    Socks
    Puppet_Sock, Mar 7, 2007
    #7
  8. ragged_hippy

    Andre Kostur Guest

    "mlimber" <> wrote in news:1173289964.171996.249820@
    30g2000cwc.googlegroups.com:

    > On Mar 7, 12:28 pm, "ragged_hippy" <> wrote:
    >> I wanted to create a vector of const references. Something like this:
    >>
    >> vector<const x&> y;
    >>
    >> where x is a class name.
    >>
    >> Is this a valid statement? Will this work if I use a reference of 'y'
    >> somewhere else?

    >
    > Did you try it? Any compiler will instantly tell you whether pointers
    > to references are allowed. (Hint: they're not, but vector requires
    > pointers to its containees.)


    Since when? Vectors contain copies of their contained objects, not
    pointers to them.
    Andre Kostur, Mar 7, 2007
    #8
  9. ragged_hippy

    mlimber Guest

    On Mar 7, 1:59 pm, Andre Kostur <> wrote:
    > "mlimber" <> wrote in news:1173289964.171996.249820@
    > 30g2000cwc.googlegroups.com:
    >
    > > On Mar 7, 12:28 pm, "ragged_hippy" <> wrote:
    > >> I wanted to create a vector of const references. Something like this:

    >
    > >> vector<const x&> y;

    >
    > >> where x is a class name.

    >
    > >> Is this a valid statement? Will this work if I use a reference of 'y'
    > >> somewhere else?

    >
    > > Did you try it? Any compiler will instantly tell you whether pointers
    > > to references are allowed. (Hint: they're not, but vector requires
    > > pointers to its containees.)

    >
    > Since when? Vectors contain copies of their contained objects, not
    > pointers to them.


    But vectors dynamically allocate the contained objects, which in turn
    requires a pointer to the value type.

    Cheers! --M
    mlimber, Mar 7, 2007
    #9
  10. ragged_hippy

    Andre Kostur Guest

    "mlimber" <> wrote in news:1173294762.235842.253370
    @j27g2000cwj.googlegroups.com:

    > On Mar 7, 1:59 pm, Andre Kostur <> wrote:
    >> "mlimber" <> wrote in news:1173289964.171996.249820@
    >> 30g2000cwc.googlegroups.com:
    >>
    >> > On Mar 7, 12:28 pm, "ragged_hippy" <> wrote:
    >> >> I wanted to create a vector of const references. Something like this:

    >>
    >> >> vector<const x&> y;

    >>
    >> >> where x is a class name.

    >>
    >> >> Is this a valid statement? Will this work if I use a reference of 'y'
    >> >> somewhere else?

    >>
    >> > Did you try it? Any compiler will instantly tell you whether pointers
    >> > to references are allowed. (Hint: they're not, but vector requires
    >> > pointers to its containees.)

    >>
    >> Since when? Vectors contain copies of their contained objects, not
    >> pointers to them.

    >
    > But vectors dynamically allocate the contained objects, which in turn
    > requires a pointer to the value type.


    Probably not. As least not individually. It requires a pointer to some
    memory. IIRC, TR1 specifies that it even must be contiguous memory.
    Whether that actually is a pointer to type (or to array of type), or merely
    a void *, that's an implementation detail. What's probably happening
    behind the scenes is that the vector allocates "sizeof(type) * capacity"
    bytes of memory, and uses placement new in each position that holds a valid
    object.

    From a vector user's point of view, all that is required is that the type
    contained in the vector is copy constructable and assignable (and that the
    constructed/assigned to object is the same as the original, which is why
    auto_ptr can't be stored in STL containers). There are no pointers
    anywhere in sight.
    Andre Kostur, Mar 8, 2007
    #10
  11. ragged_hippy

    mlimber Guest

    On Mar 7, 7:02 pm, Andre Kostur <> wrote:
    > "mlimber" <> wrote in news:1173294762.235842.253370
    > @j27g2000cwj.googlegroups.com:
    >
    >
    >
    > > On Mar 7, 1:59 pm, Andre Kostur <> wrote:
    > >> "mlimber" <> wrote in news:1173289964.171996.249820@
    > >> 30g2000cwc.googlegroups.com:

    >
    > >> > On Mar 7, 12:28 pm, "ragged_hippy" <> wrote:
    > >> >> I wanted to create a vector of const references. Something like this:

    >
    > >> >> vector<const x&> y;

    >
    > >> >> where x is a class name.

    >
    > >> >> Is this a valid statement? Will this work if I use a reference of 'y'
    > >> >> somewhere else?

    >
    > >> > Did you try it? Any compiler will instantly tell you whether pointers
    > >> > to references are allowed. (Hint: they're not, but vector requires
    > >> > pointers to its containees.)

    >
    > >> Since when? Vectors contain copies of their contained objects, not
    > >> pointers to them.

    >
    > > But vectors dynamically allocate the contained objects, which in turn
    > > requires a pointer to the value type.

    >
    > Probably not. As least not individually. It requires a pointer to some
    > memory. IIRC, TR1 specifies that it even must be contiguous memory.
    > Whether that actually is a pointer to type (or to array of type), or merely
    > a void *, that's an implementation detail. What's probably happening
    > behind the scenes is that the vector allocates "sizeof(type) * capacity"
    > bytes of memory, and uses placement new in each position that holds a valid
    > object.
    >
    > From a vector user's point of view, all that is required is that the type
    > contained in the vector is copy constructable and assignable (and that the
    > constructed/assigned to object is the same as the original, which is why
    > auto_ptr can't be stored in STL containers). There are no pointers
    > anywhere in sight.


    I don't doubt that you're right in theory, but in practice every STL
    implementation I've seen uses pointers to the containee internally and
    an error in that regard pops up if you try something like what the OP
    suggested.

    Cheers! --M
    mlimber, Mar 8, 2007
    #11
  12. On 8 Mar 2007 13:38:09 -0800, "mlimber" <> wrote:
    >> Probably not. As least not individually. It requires a pointer to some
    >> memory. IIRC, TR1 specifies that it even must be contiguous memory.
    >> Whether that actually is a pointer to type (or to array of type), or merely
    >> a void *, that's an implementation detail. What's probably happening
    >> behind the scenes is that the vector allocates "sizeof(type) * capacity"
    >> bytes of memory, and uses placement new in each position that holds a valid
    >> object.
    >>
    >> From a vector user's point of view, all that is required is that the type
    >> contained in the vector is copy constructable and assignable (and that the
    >> constructed/assigned to object is the same as the original, which is why
    >> auto_ptr can't be stored in STL containers). There are no pointers
    >> anywhere in sight.

    >
    >I don't doubt that you're right in theory, but in practice every STL
    >implementation I've seen uses pointers to the containee internally and
    >an error in that regard pops up if you try something like what the OP
    >suggested.


    There are ways to make assignable and copy-constructable objects that /behave/
    like references. See boost.Ref for instance.

    -dr
    Dave Rahardja, Mar 9, 2007
    #12
  13. ragged_hippy

    Kai-Uwe Bux Guest

    Dave Rahardja wrote:

    > On 8 Mar 2007 13:38:09 -0800, "mlimber" <> wrote:
    >>> Probably not. As least not individually. It requires a pointer to some
    >>> memory. IIRC, TR1 specifies that it even must be contiguous memory.
    >>> Whether that actually is a pointer to type (or to array of type), or
    >>> merely
    >>> a void *, that's an implementation detail. What's probably happening
    >>> behind the scenes is that the vector allocates "sizeof(type) * capacity"
    >>> bytes of memory, and uses placement new in each position that holds a
    >>> valid object.
    >>>
    >>> From a vector user's point of view, all that is required is that the
    >>> type contained in the vector is copy constructable and assignable (and
    >>> that the constructed/assigned to object is the same as the original,
    >>> which is why
    >>> auto_ptr can't be stored in STL containers). There are no pointers
    >>> anywhere in sight.

    >>
    >>I don't doubt that you're right in theory, but in practice every STL
    >>implementation I've seen uses pointers to the containee internally and
    >>an error in that regard pops up if you try something like what the OP
    >>suggested.

    >
    > There are ways to make assignable and copy-constructable objects that
    > /behave/ like references.


    No there isn't: unfortunately, the dot operator is not overloadable. If
    there was, containers for polymorphic objects would be much less of a
    problem.


    > See boost.Ref for instance.


    Does not cut it either:

    #include <boost/ref.hpp>

    struct X {
    void inc ( void ) {}
    };

    int main ( void ) {
    X value;
    X & ref = value;
    ref.inc();
    boost::reference_wrapper<X> ref_w ( ref );
    ref_w.inc(); // fails
    }


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Mar 9, 2007
    #13
  14. ragged_hippy

    Grizlyk Guest

    ragged_hippy wrote:
    >
    > But yes I immediately got his huge set of compiler errors which I didn't
    > expect.


    Try to read any C++ book about pointers, references and variables, try find
    its representation in memory.

    Internally reference can be implemented as const pointer, but for C++ users
    reference has special behaviour. After reference has been created, you can
    not access to the reference itself (can not to the reference internal
    address), only to object the reference refers to.

    It looks like reference has no default ctor
    const X &tmp; //error

    also reference can not be reassigned, as if automatically forwarding all its
    messages to own stored X object.

    const X &tmp=*static_cast<X*>(0);
    const X obj;
    tmp=obj;

    is not the same as

    const X obj;
    const X &tmp=obj;

    The purpose of reference is hiding own existance. So if you do not want the
    hiding. If you need address, use POD pointers. If you need ownership of
    dynamic memory, use RAII wrappers (as auto_ptr). Else use object itself.

    --
    Maksim A. Polyanin
    http://grizlyk1.narod.ru/cpp_new

    "In thi world of fairy tales rolls are liked olso"
    /Gnume/
    Grizlyk, Mar 9, 2007
    #14
    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. Andrew R. Thomas-Cramer
    Replies:
    8
    Views:
    531
    Roedy Green
    Jul 11, 2003
  2. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    561
    James Kanze
    Sep 4, 2007
  4. Replies:
    2
    Views:
    541
    Andrew Koenig
    Feb 9, 2009
  5. K. Frank
    Replies:
    4
    Views:
    180
Loading...

Share This Page