Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing noncopyable but movable object in std::function

Tags:

Suppose I have a functor s, which is noncopyable but movable, how can I store it in a std::function? i.e, how to make the following code compile? (using gcc 4.6)

#include <functional>
#include <iostream>

struct S
{
  S() = default;
  S(S const&) = delete;
  S& operator=(S const&) = delete;
  S(S&&) { }
  void operator()() { }
};

std::function<void()> func;

void make_func()
{
  S s;
  func = std::bind(std::move(s));  // This won't compile
}

int main()
{
  make_func();
}