#include <functional>
#include <iostream>
#include <type_traits>
using cb_t = std::function<void ()>;
template<typename CB_T, std::enable_if_t<std::is_constructible<cb_t, CB_T>::value, bool> = true>
void
on_my_write(int a, int b, CB_T&& cb) {
std::cout << "on_my_write:" << a << ", " << b << std::endl;
}
template<typename CB_T, std::enable_if_t<std::is_constructible<cb_t, CB_T>::value, bool> = true>
void foo(CB_T&& cb) {
auto b = std::bind(&on_my_write<CB_T>, std::placeholders::_1, std::placeholders::_2, std::forward<CB_T>(cb));
b(1, 2);
}
int main() {
foo([]{});
return 0;
}
使用命令行编译
g++ ./try1.cpp -std=c++17
给我的:
./try1.cpp: In instantiation of ‘void foo(CB_T&&) [with CB_T = main()::<lambda()>; typename >std::enable_if<std::is_constructible<std::function<void()>, CB_T>::value, bool>::type <anonymous> = 1]’:
./try1.cpp:39:13: required from here
./try1.cpp:34:6: error: no match for call to ‘(std::_Bind<void (*(std::_Placeholder<1>, std::_Placeholder<2>, main()::<lambda()>))(int, int, main()::<lambda()>&&)>) (int, int)’
b(1, 2);
~^~~~~~
In file included from ./try1.cpp:19:0:
/usr/include/c++/7/functional:547:2: note: candidate: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(int, int, main()::<lambda()>&&); _Bound_args = {std::_Placeholder<1>, std::_Placeholder<2>, main()::<lambda()>}]
operator()(_Args&&... __args)
^~~~~~~~
/usr/include/c++/7/functional:547:2: note: template argument deduction/substitution failed:
/usr/include/c++/7/functional:558:2: note: candidate: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(int, int, main()::<lambda()>&&); _Bound_args = {std::_Placeholder<1>, std::_Placeholder<2>, main()::<lambda()>}]
operator()(_Args&&... __args) const
^~~~~~~~
/usr/include/c++/7/functional:558:2: note: template argument deduction/substitution failed:
/usr/include/c++/7/functional:576:2: note: candidate: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(int, int, main()::<lambda()>&&); _Bound_args = {std::_Placeholder<1>, std::_Placeholder<2>, main()::<lambda()>}]
operator()(_Args&&... __args) volatile
^~~~~~~~
/usr/include/c++/7/functional:576:2: note: template argument deduction/substitution failed:
/usr/include/c++/7/functional:588:2: note: candidate: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(int, int, main()::<lambda()>&&); _Bound_args = {std::_Placeholder<1>, std::_Placeholder<2>, main()::<lambda()>}]
operator()(_Args&&... __args) const volatile
^~~~~~~~
/usr/include/c++/7/functional:588:2: note: template argument deduction/substitution failed:
但如果我将on_my_write(int a, int b, CB_T&& cb)
更改为on_my_write(int a, int b, const CB_T& cb)
,那么它将成功编译并运行。
我真的不明白为什么?
有人能向我解释一下吗?非常感谢!
我试了好几个小时都想不通。
是的,这有点棘手。std::bind
当然不神奇,它必须以某种方式存储绑定参数,并且默认情况下将它们存储为值,除非std::ref
参数要求。
这意味着在调用std::bind(...,std::forward<CB_T>(cb))
中,cb
用于使用完美转发正确地构造绑定成员变量。
但是,调用并不是用完美的转发完成的。遗憾的是,bind将传递给on_my_write
的参数总是一个l值。创建std::bind
版本来转发它并非不可能,我也不知道为什么会这样,但一个很好的原因可能是这样的转发会使多次调用绑定函子变得危险。
因此,由于std::bind
总是将参数作为l-value传递,因此您有两个选项:
- 删除完美转发并使用
on_my_write(int a, int b, const CB_T& cb)
- 删除完美转发并无论如何移动,使用
on_my_write(int a, int b, CB_T& cb)
并在内部使用std::move(cb)
来消耗它。请注意,这正是新函子应该只调用一次的情况 - 保持完美的转发,但不要将其用于此呼叫,这可以通过手动添加引用来实现:
这将得益于引用折叠规则,并且std::bind(&on_my_write<CB_T&>, std::placeholders::_1, std::placeholders::_2, std::forward<CB_T>(cb));
on_my_write(int a, int b, CB_T& cb)
将始终被调用。您仍然可以使用2。无论如何都要移动 -
选择我
忘记
std::bind
,使用lambda:
这将正确地转发template<typename CB_T, std::enable_if_t<std::is_constructible<cb_t, CB_T>::value, bool> = true> void foo(CB_T&& cb) { auto b = [&cb](auto a,auto b){return on_my_write(a,b,std::forward<CB_T>(cb));}; b(1, 2); }
cb
,此解决方案的一个额外优点是不必手动推导on_my_write