How to create a container from non-copyable items

Is there a way to use STL containers with non-copyable items?

something like that:

class noncopyable { noncopyable(noncopyable&); const noncopyable& operator=(noncopyable&); public: noncopyable(){}; }; int main() { list<noncopyable> MyList; //error C2248: 'noncopyable::noncopyable' : cannot access private member declared in class 'noncopyable' } 
+5
source share
3 answers

No, non-copyable elements cannot be in C ++ class classes.

According to the standard, clause 23.1 of clause 3: β€œThe type of objects stored in these components must satisfy the requirements of CopyConstructible types (20.1.3) and the additional requirements of Assignable types.

+15
source

One option is to create a list of pointers to elements (preferably shared_ptr). This is not exactly what you want, but it will do its job.

+15
source

Another option is to use the Boost Pointer Container library . This is very similar to the standard container std :: auto_ptrs, perhaps it would be possible: it retains exclusive ownership of its elements and cannot be copied. It also has less overhead than the standard shared_ptrs container.

+2
source

All Articles