将不可复制的闭包对象传递给 std::function 参数



在 C++14 中,lambda 表达式可以使用捕获初始值设定项从变量中移动来捕获变量。但是,这使得生成的闭包对象不可复制。如果我有一个接受std::function参数(我无法更改)的现有函数,我无法传递闭包对象,因为std::function的构造函数要求给定的函子CopyConstructible

#include <iostream>
#include <memory>
void doit(std::function<void()> f) {
    f();
}
int main()
{
    std::unique_ptr<int> p(new int(5));
    doit([p = std::move(p)] () { std::cout << *p << std::endl; });
}

这将给出以下错误:

/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/functional:1911:10: error: 
      call to implicitly-deleted copy constructor of '<lambda at test.cpp:10:7>'
            new _Functor(*__source._M_access<_Functor*>());
                ^        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/functional:1946:8: note: in
      instantiation of member function 'std::_Function_base::_Base_manager<<lambda at test.cpp:10:7>
      >::_M_clone' requested here
              _M_clone(__dest, __source, _Local_storage());
              ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/functional:2457:33: note: in
      instantiation of member function 'std::_Function_base::_Base_manager<<lambda at test.cpp:10:7>
      >::_M_manager' requested here
            _M_manager = &_My_handler::_M_manager;
                                       ^
test.cpp:10:7: note: in instantiation of function template specialization 'std::function<void
      ()>::function<<lambda at test.cpp:10:7>, void>' requested here
        doit([p = std::move(p)] () { std::cout << *p << std::endl; });
             ^
test.cpp:10:8: note: copy constructor of '' is implicitly deleted because field '' has a deleted
      copy constructor
        doit([p = std::move(p)] () { std::cout << *p << std::endl; });
              ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/unique_ptr.h:273:7: note: 
      'unique_ptr' has been explicitly marked deleted here
      unique_ptr(const unique_ptr&) = delete;
      ^

是否有合理的解决方法?

使用 Ubuntu clang 版本 3.5-1~exp1 (主干) 进行测试

有这种方法:

template< typename signature >
struct make_copyable_function_helper;
template< typename R, typename... Args >
struct make_copyable_function_helper<R(Args...)> {
  template<typename input>
  std::function<R(Args...)> operator()( input&& i ) const {
    auto ptr = std::make_shared< typename std::decay<input>::type >( std::forward<input>(i) );
    return [ptr]( Args... args )->R {
      return (*ptr)(std::forward<Args>(args)...);
    };
  }
};
template< typename signature, typename input >
std::function<signature> make_copyable_function( input && i ) {
  return make_copyable_function_helper<signature>()( std::forward<input>(i) );
}

我们创建一个指向数据的共享指针,然后创建一个可复制的 lambda 来捕获该共享指针,然后将该可复制的 Lambda 包装到请求签名的std::function中。

在上述情况下,您只需:

doit( make_copyable_function<void()>( [p = std::move(p)] () { std::cout << *p << std::endl; } ) );

稍微高级一点的版本推迟了类型擦除,并添加了一层完美的转发以减少开销:

template<typename input>
struct copyable_function {
  typedef typename std::decay<input>::type stored_input;
  template<typename... Args>
  auto operator()( Args&&... args )->
    decltype( std::declval<input&>()(std::forward<Args>(args)...) )
  {
    return (*ptr)(std::forward<Args>(args));
  }
  copyable_function( input&& i ):ptr( std::make_shared<stored_input>( std::forward<input>(i) ) ) {}
  copyable_function( copyable_function const& ) = default;
private:
  std::shared_ptr<stored_input> ptr;
};
template<typename input>
copyable_function<input> make_copyable_function( input&& i ) {
  return {std::forward<input>(i)}; 
}

这不需要您传递签名,并且在少数情况下可以稍微提高效率,但使用更晦涩的技术。

在第 14 C++中,可以更简短地说明这一点:

template< class F >
auto make_copyable_function( F&& f ) {
  using dF=std::decay_t<F>;
  auto spf = std::make_shared<dF>( std::forward<F>(f) );
  return [spf](auto&&... args)->decltype(auto) {
    return (*spf)( decltype(args)(args)... );
  };
}

完全消除了对帮助程序类型的需要。

如果闭包对象的生存期不是问题,则可以在引用包装器中传递它:

int main()
{
    std::unique_ptr<int> p(new int(5));
    auto f = [p = std::move(p)]{
        std::cout << *p << std::endl;
    };
    doit(std::cref(f));
}

这显然不适用于每个方案,但对于您的示例程序来说很好。

编辑:看看N3797(C++14工作草案)§20.9.11.2.1[func.wrap.func.con] p7,CopyConstructible要求仍然存在。我想知道是否有技术原因不能放松到MoveConstructible,或者委员会只是没有解决这个问题?

编辑:回答我自己的问题:std::functionCopyConstructible,所以包装的函子也需要CopyConstructible

如果你知道你实际上不会复制你的函数对象,那么你只需将其包装在一个使编译器认为它是可复制的类型中:

struct ThrowOnCopy {
  ThrowOnCopy() = default;
  ThrowOnCopy(const ThrowOnCopy&) { throw std::logic_error("Oops!"); }
  ThrowOnCopy(ThrowOnCopy&&) = default;
  ThrowOnCopy& operator=(ThrowOnCopy&&) = default;
};
template<typename T>
  struct FakeCopyable : ThrowOnCopy
  {
    FakeCopyable(T&& t) : target(std::forward<T>(t)) { }
    FakeCopyable(FakeCopyable&&) = default;
    FakeCopyable(const FakeCopyable& other)
    : ThrowOnCopy(other),                             // this will throw
      target(std::move(const_cast<T&>(other.target))) // never reached
    { }
    template<typename... Args>
      auto operator()(Args&&... a)
      { return target(std::forward<Args>(a)...); }
    T target;
  };

template<typename T>
  FakeCopyable<T>
  fake_copyable(T&& t)
  { return { std::forward<T>(t) }; }

// ...
doit( fake_copyable([p = std::move(p)] () { std::cout << *p << std::endl; }) );

函数模板fake_copyable创建一个包装器,该包装器根据编译器(和<type_traits>CopyConstructible,但不能在运行时复制。

如果您将FakeCopyable<X>存储在std::function中,然后最终复制std::function,您将获得抛出std::logic_error,但如果您只移动std::function一切正常。

target(std::move(const_cast<T&>(other.target)))看起来令人担忧,但该初始值设定项永远不会运行,因为基类初始值设定项将首先抛出。因此,令人担忧的const_cast永远不会真正发生,它只是让编译器满意。

相关内容

最新更新