【问题标题】:No match for call operator when std::bind-ing a std::function with a std::future当 std::bind-ing std::function 与 std::future 时调用运算符不匹配
【发布时间】:2020-05-25 11:08:35
【问题描述】:

当我尝试将std::bindstd::future<T> 转换为std::function<void(std::future<T>)> 时,我得到了一个编译器,即我不完全理解的模板错误。在我看来,std::bind 模板推导出了问题,但我也不确定如何手动正确粘贴模板参数。

#include <iostream>
#include <functional>
#include <future>
#include <utility>
#include <exception>

int main() {
    using my_result_t = int;
    std::function<void(std::future<my_result_t>)> callback {
        [] (auto result) {
            try {
                auto result_value = result.get();
                std::cout << "Result " << result_value << "\n";
            } catch (std::exception& exception) {
                std::cerr << exception.what() << "\n";
                throw;
            }
        }
    };
    std::promise<my_result_t> promise{};
    promise.set_exception(std::make_exception_ptr(std::runtime_error("Foo")));
    std::future<my_result_t> future = promise.get_future();
    auto f = std::bind(callback, std::move(future));
    f();
}
24:7: error: no match for call to ‘(std::_Bind<std::function<void(std::future<int>)>(std::future<int>)>) ()’
  24 |     f();

来自 gcc 的完整错误文本:

[ 50%] Building CXX object CMakeFiles/untitled5.dir/main.cpp.o
/home/markus/CLionProjects/untitled5/main.cpp: In function ‘int main()’:
/home/markus/CLionProjects/untitled5/main.cpp:24:7: error: no match for call to ‘(std::_Bind<std::function<void(std::future<int>)>(std::future<int>)>) ()’
   24 |     f();
      |       ^
In file included from /home/markus/CLionProjects/untitled5/main.cpp:2:
/usr/include/c++/9.3.0/functional:480:2: note: candidate: ‘template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) [with _Args = {_Args ...}; _Result = _Result; _Functor = std::function<void(std::future<int>)>; _Bound_args = {std::future<int>}]’
  480 |  operator()(_Args&&... __args)
      |  ^~~~~~~~
/usr/include/c++/9.3.0/functional:480:2: note:   template argument deduction/substitution failed:
/usr/include/c++/9.3.0/functional: In substitution of ‘template<class _Functor, class ... _Bound_args> template<class _Fn, class _CallArgs, class ... _BArgs> using _Res_type_impl = typename std::result_of<_Fn&(std::_Bind<_Functor(_Bound_args ...)>::_Mu_type<_BArgs, _CallArgs>&& ...)>::type [with _Fn = std::function<void(std::future<int>)>; _CallArgs = std::tuple<>; _BArgs = {std::future<int>}; _Functor = std::function<void(std::future<int>)>; _Bound_args = {std::future<int>}]’:
/usr/include/c++/9.3.0/functional:447:8:   required by substitution of ‘template<class _Functor, class ... _Bound_args> template<class _CallArgs> using _Res_type = std::_Bind<_Functor(_Bound_args ...)>::_Res_type_impl<_Functor, _CallArgs, _Bound_args ...> [with _CallArgs = std::tuple<>; _Functor = std::function<void(std::future<int>)>; _Bound_args = {std::future<int>}]’
/usr/include/c++/9.3.0/functional:478:9:   required from here
/usr/include/c++/9.3.0/functional:443:8: error: no type named ‘type’ in ‘class std::result_of<std::function<void(std::future<int>)>&(std::future<int>&)>’
  443 |  using _Res_type_impl
      |        ^~~~~~~~~~~~~~
/usr/include/c++/9.3.0/functional:491:2: note: candidate: ‘template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const [with _Args = {_Args ...}; _Result = _Result; _Functor = std::function<void(std::future<int>)>; _Bound_args = {std::future<int>}]’
  491 |  operator()(_Args&&... __args) const
      |  ^~~~~~~~
/usr/include/c++/9.3.0/functional:491:2: note:   template argument deduction/substitution failed:
/usr/include/c++/9.3.0/functional: In substitution of ‘template<class _Functor, class ... _Bound_args> template<class _Fn, class _CallArgs, class ... _BArgs> using _Res_type_impl = typename std::result_of<_Fn&(std::_Bind<_Functor(_Bound_args ...)>::_Mu_type<_BArgs, _CallArgs>&& ...)>::type [with _Fn = std::add_const<std::function<void(std::future<int>)> >::type; _CallArgs = std::tuple<>; _BArgs = {std::add_const<std::future<int> >::type}; _Functor = std::function<void(std::future<int>)>; _Bound_args = {std::future<int>}]’:
/usr/include/c++/9.3.0/functional:454:8:   required by substitution of ‘template<class _Functor, class ... _Bound_args> template<class _CallArgs, template<class _CallArgs, template<class> class __cv_quals> template<class _Functor, class ... _Bound_args> template<class> class __cv_quals> using _Res_type_cv = std::_Bind<_Functor(_Bound_args ...)>::_Res_type_impl<typename __cv_quals<typename std::enable_if<(bool)((std::tuple_size<_Tuple>::value + 1)), _Functor>::type>::type, _CallArgs, typename __cv_quals<_Bound_args>::type ...> [with _CallArgs = std::tuple<>; __cv_quals = std::add_const; _Functor = std::function<void(std::future<int>)>; _Bound_args = {std::future<int>}]’
/usr/include/c++/9.3.0/functional:489:9:   required from here
/usr/include/c++/9.3.0/functional:443:8: error: no type named ‘type’ in ‘class std::result_of<const std::function<void(std::future<int>)>&(const std::future<int>&)>’
  443 |  using _Res_type_impl
      |        ^~~~~~~~~~~~~~
/usr/include/c++/9.3.0/functional:509:2: note: candidate: ‘template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = std::function<void(std::future<int>)>; _Bound_args = {std::future<int>}]’
  509 |  operator()(_Args&&... __args) volatile
      |  ^~~~~~~~
/usr/include/c++/9.3.0/functional:509:2: note:   template argument deduction/substitution failed:
/usr/include/c++/9.3.0/functional: In substitution of ‘template<class _Functor, class ... _Bound_args> template<class _Fn, class _CallArgs, class ... _BArgs> using _Res_type_impl = typename std::result_of<_Fn&(std::_Bind<_Functor(_Bound_args ...)>::_Mu_type<_BArgs, _CallArgs>&& ...)>::type [with _Fn = std::add_volatile<std::function<void(std::future<int>)> >::type; _CallArgs = std::tuple<>; _BArgs = {std::add_volatile<std::future<int> >::type}; _Functor = std::function<void(std::future<int>)>; _Bound_args = {std::future<int>}]’:
/usr/include/c++/9.3.0/functional:454:8:   required by substitution of ‘template<class _Functor, class ... _Bound_args> template<class _CallArgs, template<class _CallArgs, template<class> class __cv_quals> template<class _Functor, class ... _Bound_args> template<class> class __cv_quals> using _Res_type_cv = std::_Bind<_Functor(_Bound_args ...)>::_Res_type_impl<typename __cv_quals<typename std::enable_if<(bool)((std::tuple_size<_Tuple>::value + 1)), _Functor>::type>::type, _CallArgs, typename __cv_quals<_Bound_args>::type ...> [with _CallArgs = std::tuple<>; __cv_quals = std::add_volatile; _Functor = std::function<void(std::future<int>)>; _Bound_args = {std::future<int>}]’
/usr/include/c++/9.3.0/functional:506:9:   required from here
/usr/include/c++/9.3.0/functional:443:8: error: no type named ‘type’ in ‘class std::result_of<volatile std::function<void(std::future<int>)>&(volatile std::future<int>&)>’
  443 |  using _Res_type_impl
      |        ^~~~~~~~~~~~~~
/usr/include/c++/9.3.0/functional:521:2: note: candidate: ‘template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = std::function<void(std::future<int>)>; _Bound_args = {std::future<int>}]’
  521 |  operator()(_Args&&... __args) const volatile
      |  ^~~~~~~~
/usr/include/c++/9.3.0/functional:521:2: note:   template argument deduction/substitution failed:
/usr/include/c++/9.3.0/functional: In substitution of ‘template<class _Functor, class ... _Bound_args> template<class _Fn, class _CallArgs, class ... _BArgs> using _Res_type_impl = typename std::result_of<_Fn&(std::_Bind<_Functor(_Bound_args ...)>::_Mu_type<_BArgs, _CallArgs>&& ...)>::type [with _Fn = std::add_cv<std::function<void(std::future<int>)> >::type; _CallArgs = std::tuple<>; _BArgs = {std::add_cv<std::future<int> >::type}; _Functor = std::function<void(std::future<int>)>; _Bound_args = {std::future<int>}]’:
/usr/include/c++/9.3.0/functional:454:8:   required by substitution of ‘template<class _Functor, class ... _Bound_args> template<class _CallArgs, template<class _CallArgs, template<class> class __cv_quals> template<class _Functor, class ... _Bound_args> template<class> class __cv_quals> using _Res_type_cv = std::_Bind<_Functor(_Bound_args ...)>::_Res_type_impl<typename __cv_quals<typename std::enable_if<(bool)((std::tuple_size<_Tuple>::value + 1)), _Functor>::type>::type, _CallArgs, typename __cv_quals<_Bound_args>::type ...> [with _CallArgs = std::tuple<>; __cv_quals = std::add_cv; _Functor = std::function<void(std::future<int>)>; _Bound_args = {std::future<int>}]’
/usr/include/c++/9.3.0/functional:518:9:   required from here
/usr/include/c++/9.3.0/functional:443:8: error: no type named ‘type’ in ‘class std::result_of<const volatile std::function<void(std::future<int>)>&(const volatile std::future<int>&)>’
  443 |  using _Res_type_impl
      |        ^~~~~~~~~~~~~~

【问题讨论】:

    标签: c++ c++17 future std-function


    【解决方案1】:

    std::bind 存储其参数的副本(这里是移动构造的future),然后将它们用作左值参数(该规则的一些例外情况包括引用包装器、占位符和其他绑定表达式;不适用在这里)。

    std::future 不可复制构造。也就是说,一个接受参数按值的函数,其类型不是可复制构造的,就像你的callback那样,不能用左值调用。

    为了让你的代码正常工作,你需要调整回调的签名:

    std::function<void(std::future<my_result_t>&)> callback = [] (auto& result) {
    //                                        ~^~                    ~^~
    

    不过,最好不要使用std::bind

    【讨论】:

    • 奇怪,我很难它会通过移动包装器给绑定一个右值来构造移动。
    • @Superlokkus 不能。 f() 可以出现多次。只有第一个会起作用。然而,std::thread 就是这样工作的,它在调用回调时确实使用了右值。
    • 嗯,我没有完全理解为什么我不能绑定到右值,但它仍然有很大帮助,谢谢!
    • “不过,最好不要使用 std::bind。”我假设您建议将 lambda 中的 std::function 称为 f?
    • @Superlokkus 您正在在从std::bind 返回的可调用对象内从右值移动构造参数的副本,但应用operator() 它必须使用该副本的左值,因此可以多次使用它们(可以多次调用该可调用对象)。是的,我的意思是直接使用 lambda,如果没有特殊需要,甚至不用将它们包装在 std::function 中。
    【解决方案2】:

    值得一提的是,实际使用的解决方案是将绑定替换为 lambda。我在问题中忘记提到的是,将回调的签名更改为引用对我来说不是一个合适的选择,因为 f 可调用对象将被复制/移动到异步队列中,即反应器模式,即boost::asio::post。一个参考会为那个未来的一生带来一生的挑战。我没有首先提到它,因为我希望std::bind 仍然可以实现,但@piotr-skotnicki 很好地解释了为什么它不可能。因此,为了其他搜索的完整性,事不宜迟:

    std::future<my_result_t> future = promise.get_future();
    auto f = [callback, future = std::move(future)] () mutable{
        callback(std::move(future));
    };
    f();
    

    【讨论】:

      猜你喜欢
      • 2016-12-18
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2013-04-16
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多