gpt4 book ai didi

c++ - 将 std 绑定(bind)传递到函数映射的问题

转载 作者:行者123 更新时间:2023-11-30 03:58:49 25 4
gpt4 key购买 nike

我使用 std::function 创建了一个函数映射,当我将一个普通函数传递给它时它可以工作,但是当我尝试传递 std::bind< 的结果时 我有问题,我会这样做,遵循我的代码:

#include <iostream>
#include <functional>
#include <memory>
#include <map>
#include <tr1/functional>

class A {
int x;
public:
A(int v):x(v){}
int getX() const {return x;}
};

class B {
int x;
public:
B(int v):x(v){}
int getX() const {return x;}
};

class C {
public:
C(){}
std::shared_ptr<A> calc(std::shared_ptr<B> b) {
std::shared_ptr<A> pa (new A(b->getX()));
std::cout << "Class C calc: " << pa->getX() << '\n';
return pa;
}

};

std::shared_ptr<A> calc_out(std::shared_ptr<B> b) {
std::shared_ptr<A> pa (new A(b->getX()*2));
std::cout << "calc_out: " << pa->getX() << '\n';
return pa;
}

int main(int argc, char **argv) {
using namespace std::tr1::placeholders;
C *c = new C();
std::map<int, std::function<std::shared_ptr<A>(std::shared_ptr<B>)>> tmap;
auto icc_fn = std::mem_fn(&C::calc);
auto b_fn = std::bind(icc_fn, c, _1);

tmap[1] = calc_out;

//FIXME: THE PROBLEM IS HERE
tmap[2] = b_fn;

std::shared_ptr<B> pb (new B(10));

std::shared_ptr<A> a1(b_fn(pb));
std::cout << "A1: " << a1->getX() << '\n';

std::shared_ptr<A> a2(tmap[1](pb));
std::cout << "A2: " << a2->getX() << '\n';

return 0;
}

你怎么看我以同样的方式使用函数 b_fn(pb)tmap[1](pb)

当我注释这一行时 tmap[1] = b_fn; 程序工作正常,但是当它有这一行时我得到错误:

/home/alex/Tests/cppfunc/main.cpp:48:11: error: no match for ‘operator=’ (operand types are ‘std::map<int, std::function<std::shared_ptr<A>(std::shared_ptr<B>)> >::mapped_type {aka std::function<std::shared_ptr<A>(std::shared_ptr<B>)>}’ and ‘std::_Bind<std::_Mem_fn<std::shared_ptr<A> (C::*)(std::shared_ptr<B>)>(C*, std::tr1::_Placeholder<1>)>’)
tmap[1] = b_fn;
^
/home/alex/Tests/cppfunc/main.cpp:48:11: note: candidates are:
In file included from /home/alex/Tests/cppfunc/main.cpp:2:0:
/usr/include/c++/4.9/functional:2241:7: note: std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(const std::function<_Res(_ArgTypes ...)>&) [with _Res = std::shared_ptr<A>; _ArgTypes = {std::shared_ptr<B>}]
operator=(const function& __x)
^
/usr/include/c++/4.9/functional:2241:7: note: no known conversion for argument 1 from ‘std::_Bind<std::_Mem_fn<std::shared_ptr<A> (C::*)(std::shared_ptr<B>)>(C*, std::tr1::_Placeholder<1>)>’ to ‘const std::function<std::shared_ptr<A>(std::shared_ptr<B>)>&’
/usr/include/c++/4.9/functional:2259:7: note: std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(std::function<_Res(_ArgTypes ...)>&&) [with _Res = std::shared_ptr<A>; _ArgTypes = {std::shared_ptr<B>}]
operator=(function&& __x)
^
/usr/include/c++/4.9/functional:2259:7: note: no known conversion for argument 1 from ‘std::_Bind<std::_Mem_fn<std::shared_ptr<A> (C::*)(std::shared_ptr<B>)>(C*, std::tr1::_Placeholder<1>)>’ to ‘std::function<std::shared_ptr<A>(std::shared_ptr<B>)>&&’
/usr/include/c++/4.9/functional:2273:7: note: std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(std::nullptr_t) [with _Res = std::shared_ptr<A>; _ArgTypes = {std::shared_ptr<B>}; std::nullptr_t = std::nullptr_t]
operator=(nullptr_t)
^
/usr/include/c++/4.9/functional:2273:7: note: no known conversion for argument 1 from ‘std::_Bind<std::_Mem_fn<std::shared_ptr<A> (C::*)(std::shared_ptr<B>)>(C*, std::tr1::_Placeholder<1>)>’ to ‘std::nullptr_t’
/usr/include/c++/4.9/functional:2302:2: note: template<class _Functor> std::function<_Res(_ArgTypes ...)>::_Requires<std::__and_<std::__not_<std::is_same<typename std::decay<_Tp>::type, std::function<_Res(_ArgTypes ...)> > >, std::__or_<std::is_void<_Tp>, std::is_convertible<std::function<_Res(_ArgTypes ...)>::_Invoke<typename std::decay<_Tp>::type>, _Res> > >, std::function<_Res(_ArgTypes ...)>&> std::function<_Res(_ArgTypes ...)>::operator=(_Functor&&) [with _Functor = _Functor; _Res = std::shared_ptr<A>; _ArgTypes = {std::shared_ptr<B>}]
operator=(_Functor&& __f)
^
/usr/include/c++/4.9/functional:2302:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:2311:2: note: template<class _Functor> std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(std::reference_wrapper<_Tp>) [with _Functor = _Functor; _Res = std::shared_ptr<A>; _ArgTypes = {std::shared_ptr<B>}]
operator=(reference_wrapper<_Functor> __f) noexcept
^
/usr/include/c++/4.9/functional:2311:2: note: template argument deduction/substitution failed:
/home/alex/Tests/cppfunc/main.cpp:48:11: note: ‘std::_Bind<std::_Mem_fn<std::shared_ptr<A> (C::*)(std::shared_ptr<B>)>(C*, std::tr1::_Placeholder<1>)>’ is not derived from ‘std::reference_wrapper<_Tp>’
tmap[1] = b_fn;
^
CMakeFiles/cppfunc.dir/build.make:54: recipe for target 'CMakeFiles/cppfunc.dir/main.cpp.o' failed
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/cppfunc.dir/all' failed
Makefile:117: recipe for target 'all' failed
make[2]: *** [CMakeFiles/cppfunc.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/cppfunc.dir/all] Error 2
make: *** [all] Error 2

最佳答案

你的问题是混合<functional><tr1/functional> .如果删除 #include <tr1/functional>并更改 using namespace std::tr1::placeholders;using namespace std::placeholders;您的代码编译正确。 ( Example .)

不幸的是,TR1 header 中的占位符对象与 std::bind 不兼容。在主要<functional> header ;他们将与 std::tr1::bind 合作,如果你不能依赖 std::bind,这是你的选择在您的实现中正常工作。

关于c++ - 将 std 绑定(bind)传递到函数映射的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27252352/

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