b02910e3   
   From: dave@boostpro.com   
      
   on Sun Oct 30 2011, Ralph Zhang wrote:   
      
   > 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    
   > #include    
   >   
   > struct S   
   > {   
   > S() = default;   
   > S(S const&) = delete;   
   > S& operator=(S const&) = delete;   
   > S(S&&) { }   
   > void operator()() { }   
   > };   
   >   
   > std::function func;   
   >   
   > void make_func()   
   > {   
   > S s;   
   > func = std::bind(std::move(s)); // This won't compile   
   > }   
   >   
   > int main()   
   > {   
   > make_func();   
   > func();   
   > }   
      
   You can't do it directly. std::function's job is to hide from the type   
   system all details about the object it's storing, e.g. how it is called,   
   destroyed, and copied. That includes the detail of whether or not the   
   stored object *can* be copied. It works by generating functions to   
   call/copy/delete the stored object at the point where the object is   
   stored, and later calling those functions indirectly through function   
   pointers (or vtables; same thing). If those functions can't compile   
   (say because the stored object is noncopyable), compilation fails.   
      
   You have two alternatives:   
      
   1. Build what is essentially a std::move_function, a move-only function   
    wrapper, and use that.   
      
   2. Build a copyable wrapper around a shared_ptr to your move-only   
    function object, and store that wrapper in std::function. Of course   
    that means if your underlying function object has any mutable state,   
    the resulting objects don't have value semantics. Considering that   
    your operator() is non-const, that could be a worry.   
      
      
   By the way, a shorter and equally-valid test would be simply:   
      
    std::function func = S();   
      
   Cheers,   
      
   --   
   Dave Abrahams   
   BoostPro Computing   
   http://www.boostpro.com   
      
      
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|