peter_foelsche | 21 Nov 21:28

std::vector and default constructor

It would be helpful to have something like std::vector which allows one to pass some argument which is in turn passed to the constructor of every element so that not the default constructor is being called.

This would be helpful for e.g. objects located in memory created by memory mapped files.

 

Peter

 

"The first point (using an init() function in preference to a constructor) is bogus. Using constructors and exception handling is a more general and systematic way of dealing with resource acquisition and initialization errors. This style is a relic of pre-exception C++." -- Stroustrup

 

_______________________________________________
Boost-users mailing list
Boost-users <at> lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Andrew Sutton | 21 Nov 21:43

Re: std::vector and default constructor

It would be helpful to have something like std::vector which allows one to pass some argument which is in turn passed to the constructor of every element so that not the default constructor is being called.

This would be helpful for e.g. objects located in memory created by memory mapped files.

I think you could probably do this by building a custom allocator, so you could write something like:

vector<T, default_value_allocator<T, some_value> > v;

This could just be a simple wrapper around the default allocator.

Andrew Sutton
andrew.n.sutton <at> gmail.com
_______________________________________________
Boost-users mailing list
Boost-users <at> lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
peter_foelsche | 21 Nov 22:29

Re: std::vector and default constructor

what about if I have more than one memory mapped file and multiple such vectors in separate memory mapped io areas?

I think the method you're suggesting uses global variables, doesn't it?

 

Peter

 

From: boost-users-bounces <at> lists.boost.org [mailto:boost-users-bounces <at> lists.boost.org] On Behalf Of Andrew Sutton
Sent: Friday, November 21, 2008 12:44
To: boost-users <at> lists.boost.org
Subject: Re: [Boost-users] std::vector and default constructor

 

It would be helpful to have something like std::vector which allows one to pass some argument which is in turn passed to the constructor of every element so that not the default constructor is being called.

This would be helpful for e.g. objects located in memory created by memory mapped files.

I think you could probably do this by building a custom allocator, so you could write something like:

vector<T, default_value_allocator<T, some_value> > v;

This could just be a simple wrapper around the default allocator.


Andrew Sutton
andrew.n.sutton <at> gmail.com

_______________________________________________
Boost-users mailing list
Boost-users <at> lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Andrew Sutton | 22 Nov 00:20

Re: std::vector and default constructor

what about if I have more than one memory mapped file and multiple such vectors in separate memory mapped io areas?

I think the method you're suggesting uses global variables, doesn't it

No. I'm thinking about templating the allocator with a make_-style function that reutnrs an object of type T and then copying its value into the object that you're allocating. So somewhere in the allocator you'd have:

new T(make_func())

This also won't result in the copy that it might seem to require. Most (all?) compilers will optimize it out.

Andrew Sutton
andrew.n.sutton <at> gmail.com
_______________________________________________
Boost-users mailing list
Boost-users <at> lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Gmane