Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer index-able RAII container for non-copyable type

Is there a standard container that has the same general API as vector<T> but that populates new locations via direct default construction?


Background:

I have a type that disallows copying but has a default constructor and what I really want to do is this:

vector<NoCopy> bag(some_size);

// use bag[i]'s

return; // bag & contents get correctly cleaned up. 

However, this doesn't work because vector<T>(int) is implemented in terms of default constructing an object and then copying it into each of the new locations.


Edit: Not C++0xB (a.k.a. C++11)

like image 669
BCS Avatar asked Jan 18 '23 04:01

BCS


1 Answers

One option would be to upgrade to a C++11-compliant Standard Library implementation.

In C++11, the vector(size_type) constructor default constructs N elements into the container. It neither copies nor moves any elements.

Visual C++ 2010 does not support this C++11 feature; I believe the Visual C++ 11 Developer Preview does correctly support it though. I do not know whether recent versions of libstdc++ support this; I would suspect that libc++ does.

like image 123
James McNellis Avatar answered Jan 29 '23 19:01

James McNellis