dimanche 26 mars 2017

Partially std::move a vector? Or how to split without new memory allocation?

When we move std::vector we just steal its content. So this code:

std::vector<MyClass> v{ std::move(tmpVec) };

will not allocate new memory, will not call any of constructors of MyClass.

But what if I want to split a temporary vector? In theory, I could steal the content as we did before and distribute it among new vectors. In practice I can't do this. The best so far solution I found is to use std::move() from <algorithm> header. But here the operator new will be called for every new vector. Additionally, move constructor (if available) will be called for every element we move.

What else can I do (c++17 counts)?

Aucun commentaire:

Enregistrer un commentaire