gpt4 book ai didi

c++ - 将具有 unique_ptr 参数的函数绑定(bind)到 std::function

转载 作者:可可西里 更新时间:2023-11-01 17:37:01 26 4
gpt4 key购买 nike

我正在尝试使以下代码工作:

#include <cstdio>
#include <functional>
#include <string>
#include <memory>

using namespace std;

class Foo {
public:
Foo(): m_str("foo") { }

void f1(string s1, string s2, unique_ptr<Foo> p)
{
printf("1: %s %s %s\n", s1.c_str(), s2.c_str(), p->str());
}

void f2(string s1, string s2, Foo* p)
{
printf("2: %s %s %s\n", s1.c_str(), s2.c_str(), p->str());
}

const char* str() const { return m_str.c_str(); }

private:
string m_str;
};

int main()
{
string arg1 = "arg1";
string arg2 = "arg2";
Foo s;
unique_ptr<Foo> ptr(new Foo);


//function<void()> f(bind(&Foo::f1, &s, arg1, arg2, std::move(ptr)));
function<void()> f(bind(&Foo::f2, &s, arg1, arg2, ptr.release()));

f();
}

调用 f() 绑定(bind)到 Foo::f2(最后一个参数是原始指针)工作正常,但将它绑定(bind)到 Foo::f1 导致编译错误:

test.cpp: In function ‘int main()’:
test.cpp:36:70: error: no matching function for call to ‘std::function<void()>::function(std::_Bind_helper<false, void (Foo::*)(std::basic_string<char>, std::basic_string<char>, std::unique_ptr<Foo>), Foo*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::unique_ptr<Foo, std::default_delete<Foo> > >::type)’
function<void()> f(bind(&Foo::f1, &s, arg1, arg2, std::move(ptr)));
^
test.cpp:36:70: note: candidates are:
In file included from test.cpp:2:0:
/usr/include/c++/4.8.2/functional:2251:2: note: template<class _Functor, class> std::function<_Res(_ArgTypes ...)>::function(_Functor)
function(_Functor);
^
/usr/include/c++/4.8.2/functional:2251:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8.2/functional:2226:7: note: std::function<_Res(_ArgTypes ...)>::function(std::function<_Res(_ArgTypes ...)>&&) [with _Res = void; _ArgTypes = {}]
function(function&& __x) : _Function_base()
^
/usr/include/c++/4.8.2/functional:2226:7: note: no known conversion for argument 1 from ‘std::_Bind_helper<false, void (Foo::*)(std::basic_string<char>, std::basic_string<char>, std::unique_ptr<Foo>), Foo*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::unique_ptr<Foo, std::default_delete<Foo> > >::type {aka std::_Bind<std::_Mem_fn<void (Foo::*)(std::basic_string<char>, std::basic_string<char>, std::unique_ptr<Foo>)>(Foo*, std::basic_string<char>, std::basic_string<char>, std::unique_ptr<Foo>)>}’ to ‘std::function<void()>&&’
/usr/include/c++/4.8.2/functional:2429:5: note: std::function<_Res(_ArgTypes ...)>::function(const std::function<_Res(_ArgTypes ...)>&) [with _Res = void; _ArgTypes = {}]
function<_Res(_ArgTypes...)>::
^
/usr/include/c++/4.8.2/functional:2429:5: note: no known conversion for argument 1 from ‘std::_Bind_helper<false, void (Foo::*)(std::basic_string<char>, std::basic_string<char>, std::unique_ptr<Foo>), Foo*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::unique_ptr<Foo, std::default_delete<Foo> > >::type {aka std::_Bind<std::_Mem_fn<void (Foo::*)(std::basic_string<char>, std::basic_string<char>, std::unique_ptr<Foo>)>(Foo*, std::basic_string<char>, std::basic_string<char>, std::unique_ptr<Foo>)>}’ to ‘const std::function<void()>&’
/usr/include/c++/4.8.2/functional:2206:7: note: std::function<_Res(_ArgTypes ...)>::function(std::nullptr_t) [with _Res = void; _ArgTypes = {}; std::nullptr_t = std::nullptr_t]
function(nullptr_t) noexcept
^
/usr/include/c++/4.8.2/functional:2206:7: note: no known conversion for argument 1 from ‘std::_Bind_helper<false, void (Foo::*)(std::basic_string<char>, std::basic_string<char>, std::unique_ptr<Foo>), Foo*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::unique_ptr<Foo, std::default_delete<Foo> > >::type {aka std::_Bind<std::_Mem_fn<void (Foo::*)(std::basic_string<char>, std::basic_string<char>, std::unique_ptr<Foo>)>(Foo*, std::basic_string<char>, std::basic_string<char>, std::unique_ptr<Foo>)>}’ to ‘std::nullptr_t’
/usr/include/c++/4.8.2/functional:2199:7: note: std::function<_Res(_ArgTypes ...)>::function() [with _Res = void; _ArgTypes = {}]
function() noexcept
^
/usr/include/c++/4.8.2/functional:2199:7: note: candidate expects 0 arguments, 1 provided

我做错了什么?

我正在使用 gcc 4.8.2 和 -std=c++0x(-std=c++11 也失败)标志。

最佳答案

其他答案中描述的绑定(bind)问题(截至撰写本文时)并不是编译器在问题中提示的问题。问题是 std::function 必须是 CopyConstructible,这要求它的参数(将由函数存储)也是 CopyConstructible。

来自标准【20.9.11.2类模板函数】

template<class F> function(F f); 
template <class F, class A> function(allocator_arg_t, const A& a, F f);

Requires: F shall be CopyConstructible. f shall be Callable (20.9.11.2) for argument types ArgTypes and return type R . The copy constructor and destructor of A shall not throw exceptions...

考虑这个甚至不包括绑定(bind)的例子:

#include <functional>
#include <memory>

using namespace std;

struct NonCopyableFunctor {
NonCopyableFunctor(){}
NonCopyableFunctor(const NonCopyableFunctor &) = delete;
NonCopyableFunctor(NonCopyableFunctor &&){}
void operator()(){}
};

int main()
{
NonCopyableFunctor fun;
function<void()> vfun(move(fun)); // even though I move here,
// it still complains about a copy going on elsewhere.
}

这是 clang 的输出:

[orm@localhost ~]$ clang++ -std=c++11 bound_unique.cc 
In file included from bound_unique.cc:1:
/usr/bin/../lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2/functional:1911:10: error: call to deleted constructor of 'NonCopyableFunctor'
new _Functor(*__source._M_access<_Functor*>());
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2/functional:1946:8: note: in instantiation of member function
'std::_Function_base::_Base_manager<NonCopyableFunctor>::_M_clone' requested here
_M_clone(__dest, __source, _Local_storage());
^
/usr/bin/../lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2/functional:2453:33: note: in instantiation of member function
'std::_Function_base::_Base_manager<NonCopyableFunctor>::_M_manager' requested here
_M_manager = &_My_handler::_M_manager;
^
bound_unique.cc:16:20: note: in instantiation of function template specialization 'std::function<void ()>::function<NonCopyableFunctor, void>' requested here
function<void()> vfun(move(fun));
^
bound_unique.cc:8:3: note: function has been explicitly marked deleted here
NonCopyableFunctor(const NonCopyableFunctor &) = delete;
^
1 error generated.

请注意,如果您绑定(bind)一个 unique_ptr,生成的绑定(bind)对象将是不可复制的。 Bind 仍然会编译。

关于c++ - 将具有 unique_ptr 参数的函数绑定(bind)到 std::function<void()>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20268482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com