jeudi 31 août 2017

moving a unique_ptr into a vector

I have a function that looks similar to this

void resolve_dependencies(std::vector<std::unique_ptr<LoadedPlugin>>& result, std::vector<std::unique_ptr<LoadedPlugin>>& source, std::unique_ptr<LoadedPlugin>&& plugin)
{
    auto p = plugin.get();
    auto cur_pos = result.insert(result.begin(), std::move(plugin));
    ... more code ...
}

It moves the loaded plugin objects around in the result vector, doing a topological sort on them. My problem is that I get the following error on the result.insert line.

Error   C2280   'std::unique_ptr<LoadedPlugin,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function

It is clear to me that something is trying to copy any of the unique_ptrs, but I cannot figure out why as I move the unique_ptr into the vector. Since I insert it with the insert function, the vector will try to move all objects to the right of it to a new memory location but should that not bve handled precisely through a move operation rather than a copy operation since c++11?

I have the same issue if I try to push_back the unique pointer instead. All I can think of is that something regarding my LoadedPlugin object is triggering this strange behaviour, but in this case nothing should be called on it right? The compiler and standard library is the ones that come with Microsoft Visual Studio Community 2017 Preview, Version 15.30, Preview 7.0.

Aucun commentaire:

Enregistrer un commentaire