mercredi 28 janvier 2015

How to store non-copyable std::function into a container?

I want to store callbacks in a vector or another container in C++11.


One way to do so would be to store a vector of std::function. This works well for lambda or std::bind with copyable arguments.


However, if there is one non copyable (movable only) argument, it will fail due to the conversion from the lambda/std::bind internal type to the std::function...



#include <vector>

class NonCopyable {
public:
NonCopyable() = default;
NonCopyable(const NonCopyable &) = delete;
NonCopyable(NonCopyable &&) = default;
};

int main() {
std::vector<std::function<void()>> callbacks;
callbacks.emplace_back([] {});

NonCopyable tmp;
callbacks.emplace_back(std::bind([](const NonCopyable &) {}, std::move(tmp)));
// When converting the object returned by std::bind to a std::function, a copy of the arguments happen so this code cannot compile.
return 0;


}


Is there a way to move std::bind arguments into the std::function instead of copying them?


Aucun commentaire:

Enregistrer un commentaire