472,146 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

vector of const references. Is it possible?

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.

Mar 7 '07 #1
13 9434
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.
Mar 7 '07 #2
On Mar 7, 12:28 pm, "ragged_hippy" <pranes...@gmail.comwrote:
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

Mar 7 '07 #3

"ragged_hippy" <pr*******@gmail.comwrote in message
news:11**********************@s48g2000cws.googlegr oups.com...
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
Mar 7 '07 #4
On Mar 7, 11:58 am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"ragged_hippy" <pranes...@gmail.comwrote in message

news:11**********************@s48g2000cws.googlegr oups.com...
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.

Mar 7 '07 #5
"ragged_hippy" <pr*******@gmail.comwrote in message
news:11*********************@j27g2000cwj.googlegro ups.com...
On Mar 7, 11:58 am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
>"ragged_hippy" <pranes...@gmail.comwrote in message

news:11**********************@s48g2000cws.googleg roups.com...
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*>
Mar 7 '07 #6
On Mar 7, 1:09 pm, "ragged_hippy" <pranes...@gmail.comwrote:
[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

Mar 7 '07 #7
"mlimber" <ml*****@gmail.comwrote in news:1173289964.171996.249820@
30g2000cwc.googlegroups.com:
On Mar 7, 12:28 pm, "ragged_hippy" <pranes...@gmail.comwrote:
>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.
Mar 7 '07 #8
On Mar 7, 1:59 pm, Andre Kostur <nntps...@kostur.netwrote:
"mlimber" <mlim...@gmail.comwrote in news:1173289964.171996.249820@
30g2000cwc.googlegroups.com:
On Mar 7, 12:28 pm, "ragged_hippy" <pranes...@gmail.comwrote:
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

Mar 7 '07 #9
"mlimber" <ml*****@gmail.comwrote in news:1173294762.235842.253370
@j27g2000cwj.googlegroups.com:
On Mar 7, 1:59 pm, Andre Kostur <nntps...@kostur.netwrote:
>"mlimber" <mlim...@gmail.comwrote in news:1173289964.171996.249820@
30g2000cwc.googlegroups.com:
On Mar 7, 12:28 pm, "ragged_hippy" <pranes...@gmail.comwrote:
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.
Mar 8 '07 #10
On Mar 7, 7:02 pm, Andre Kostur <nntps...@kostur.netwrote:
"mlimber" <mlim...@gmail.comwrote in news:1173294762.235842.253370
@j27g2000cwj.googlegroups.com:
On Mar 7, 1:59 pm, Andre Kostur <nntps...@kostur.netwrote:
"mlimber" <mlim...@gmail.comwrote in news:1173289964.171996.249820@
30g2000cwc.googlegroups.com:
On Mar 7, 12:28 pm, "ragged_hippy" <pranes...@gmail.comwrote:
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

Mar 8 '07 #11
On 8 Mar 2007 13:38:09 -0800, "mlimber" <ml*****@gmail.comwrote:
>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
Mar 9 '07 #12
Dave Rahardja wrote:
On 8 Mar 2007 13:38:09 -0800, "mlimber" <ml*****@gmail.comwrote:
>>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<Xref_w ( ref );
ref_w.inc(); // fails
}
Best

Kai-Uwe Bux
Mar 9 '07 #13
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/
Mar 9 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by The Directive | last post: by
1 post views Thread by red floyd | last post: by
7 posts views Thread by raj | last post: by
2 posts views Thread by matthias_k | last post: by
2 posts views Thread by vineoff | last post: by
reply views Thread by Chris Thomasson | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.