Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > vector of const references. Is it possible?

Reply
Thread Tools

vector of const references. Is it possible?

 
 
mlimber
Guest
Posts: n/a
 
      03-08-2007
On Mar 7, 7:02 pm, Andre Kostur <(E-Mail Removed)> wrote:
> "mlimber" <(E-Mail Removed)> wrote in news:1173294762.235842.253370
> @j27g2000cwj.googlegroups.com:
>
>
>
> > On Mar 7, 1:59 pm, Andre Kostur <(E-Mail Removed)> wrote:
> >> "mlimber" <(E-Mail Removed)> wrote in news:1173289964.171996.249820@
> >> 30g2000cwc.googlegroups.com:

>
> >> > On Mar 7, 12:28 pm, "ragged_hippy" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Dave Rahardja
Guest
Posts: n/a
 
      03-09-2007
On 8 Mar 2007 13:38:09 -0800, "mlimber" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      03-09-2007
Dave Rahardja wrote:

> On 8 Mar 2007 13:38:09 -0800, "mlimber" <(E-Mail Removed)> 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
 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      03-09-2007
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/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM



Advertisments