我需要将一个被删除复制构造函数的结构体绑定到一个函数上。我已经将我尝试实现的内容简化为以下最小示例:
struct Bar {
int i;
Bar() = default;
Bar(Bar&&) = default;
Bar(const Bar&) = delete;
Bar& operator=(const Bar&) = delete;
};
void foo(Bar b) {
std::cout << b.i << std::endl;
}
int main()
{
Bar b;
b.i = 10;
std::function<void()> a = std::bind(foo, std::move(b)); // ERROR
a();
return 0;
}
编译器只给我嚎啕大哭和牙齿咬合的声音:
test.cpp:22:27: error: no viable conversion from 'typename _Bind_helper<__is_socketlike<void (&)(Bar)>::value, void (&)(Bar), Bar>::type' (aka '_Bind<__func_type (typename decay<Bar>::type)>') to 'std::function<void ()>'
std::function<void()> a = std::bind(foo, std::move(b));
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.1.0/../../../../include/c++/5.1.0/functional:2013:7: note: candidate constructor not viable: no known conversion from 'typename _Bind_helper<__is_socketlike<void (&)(Bar)>::value, void (&)(Bar),
Bar>::type' (aka '_Bind<__func_type (typename decay<Bar>::type)>') to 'nullptr_t' for 1st argument
function(nullptr_t) noexcept
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.1.0/../../../../include/c++/5.1.0/functional:2024:7: note: candidate constructor not viable: no known conversion from 'typename _Bind_helper<__is_socketlike<void (&)(Bar)>::value, void (&)(Bar),
Bar>::type' (aka '_Bind<__func_type (typename decay<Bar>::type)>') to 'const std::function<void ()> &' for 1st argument
function(const function& __x);
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.1.0/../../../../include/c++/5.1.0/functional:2033:7: note: candidate constructor not viable: no known conversion from 'typename _Bind_helper<__is_socketlike<void (&)(Bar)>::value, void (&)(Bar),
Bar>::type' (aka '_Bind<__func_type (typename decay<Bar>::type)>') to 'std::function<void ()> &&' for 1st argument
function(function&& __x) : _Function_base()
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.1.0/../../../../include/c++/5.1.0/functional:2058:2: note: candidate template ignored: substitution failure [with _Functor = std::_Bind<void (*(Bar))(Bar)>]: no matching function for call to object of
type 'std::_Bind<void (*(Bar))(Bar)>'
function(_Functor);
^
1 error generated.
我想问一下是否有任何方法可以让我将Bar绑定到foo,同时保持Bar只能移动。
编辑:
还请考虑以下代码,其中变量b的生命周期在调用a
之前结束:
int main()
{
std::function<void()> a;
{
Bar b;
b.i = 10;
a = std::bind(foo, std::move(b)); // ERROR
}
a();
return 0;
}
task_once
函数的void
返回类型出现了问题。已经修复:请查看新的实时示例。只需将std::function
替换为task_once
,并使用可移动捕获的可变 lambda 表达式代替std::bind
。通常情况下,std::bind
不是一个好主意。 - Yakk - Adam Nevraumont