Is it possible to initialize a vector of vectors of non-copyable objects?

604 Views Asked by At

Is it possible to initialize a vector of vectors of non-copyable objects?

class obj : private boost::noncopyable {
    // ...
};

vector<vector<obj> > v(10); // OK
for(int i = 0; i < v.size(); ++i)
    v[i].resize(10); // ERROR

also

vector<vector<obj> > v(10, vector<obj>(10)); // ERROR

I understand why the above code won't compile. What I'm asking is whether there's a workaround. It seems that if std::vector had a member function like resize_from_zero that didn't need a copy constructor, then this would be doable.

(My question is about C++03, if this makes a difference)

1

There are 1 best solutions below

0
On BEST ANSWER

This is not possible; C++03 requires that elements of a vector be CopyConstructible and Assignable.

Rreference: C++03 [lib.containers.requirements]/3

The type of objects stored in these components must meet the requirements of CopyConstructible types (20.1.3), and the additional requirements of Assignable types.

where "these components" means deque, list, queue, stack, vector, map, set, bitset.

(There may or may not be some way to have it appear to work on a particular compiler, but that is beyond the scope of Standard C++).