我如何使用std :: function的多态性? [英] How can I use polymorphism with std::function?
问题描述
class A {}
class B:public A {}
我想使用 std :: function
接收 A 类型的任何东西,但为其分配接收从 A 继承的类的方法(如 B ) 。
void myFun(B bbbb){}
std :: function< void(A) > blah = std :: bind(myFun,_1);
这显然不起作用,因为编译器不会隐式地向下转发。
但是我怎么能这样做?基本上我想要保存一些基本的std :: function类型的映射,并且在每个映射值中都会将 std :: function
保存为派生类型,如 B 。
有没有办法将一个转换运算符绑定到占位符?
好吧,我刚刚完成了一个解决方法。
编译器不会让你隐式地下注,所以我绑定了一个cast方法。
因此,为了保持它的所有泛型和模板化,它就像这样:
首先,获取函数参数类型的帮助器类: p>
模板< typename T>
class GetFunctionArgumentVal;
模板< class T,typename U>
class GetFunctionArgumentVal< std :: function< U(T)>>
{
public:
typedef T arg;
typedef U returnVal;
};
然后,一个使用static_cast(保持编译时类型安全性)的cast操作符,然后调用函数与派生类:
template< typename FUNCTION,typename BASE>
void castAndCall(FUNCTION bf,BASE& temp)
{
bf(static_cast< GetFunctionArgumentVal< FUNCTION> :: arg>(temp));
$ / code>
用法示例:
class A {};
class B:public A {};
class C:public A {};
void targetB(B& temp)
{
}
void targetC(C& temp)
{
}
std :: function< void(A&)>自动对焦;
std :: function< void(B&)> bf = targetB;
std :: function< void(C&)> cf = targetC;
B b;
C c;
af = std :: bind(castAndCall< decltype(bf),A>,bf,std :: placeholders :: _ 1);
af(b);
af = std :: bind(castAndCall< decltype(cf),A>,cf,std :: placeholders :: _ 1);
af(c);
Let's say I have 2 classes:
class A {}
class B : public A {}
And i want to use an std::function
the receives anything of type A, but with assign to it methods that receive classes that inherit from A (like B).
void myFun(B bbbb) {}
std::function<void(A)> blah = std::bind(myFun, _1);
This obviously doesn't work, because the compiler won't just downcast implicitly.
But how can I do something like this ? Basically I want to hold a map of some basic std::function type, and in each mapped value it will hold an std::function
to a derived type like B.
Is there a way to bind a cast operator to the placeholder ?
OK, well i've just done a workaround in the end.
The compiler won't let you downcast implicitly, so I've binded a cast method.
So, to keep it all generic and templated, it goes like this:
First, a helper class to get the function argument type:
template <typename T>
class GetFunctionArgumentVal;
template <class T, typename U >
class GetFunctionArgumentVal<std::function<U(T)>>
{
public:
typedef T arg;
typedef U returnVal;
};
Then, a cast operator that casts using static_cast (keeps compile time type safety), then calls the function with the derived class:
template <typename FUNCTION, typename BASE>
void castAndCall(FUNCTION bf, BASE& temp)
{
bf(static_cast< GetFunctionArgumentVal<FUNCTION>::arg >(temp));
}
Usage example:
class A {};
class B : public A {};
class C : public A {};
void targetB(B& temp)
{
}
void targetC(C& temp)
{
}
std::function<void(A &)> af;
std::function<void(B &)> bf = targetB;
std::function<void(C &)> cf = targetC;
B b;
C c;
af = std::bind(castAndCall<decltype(bf),A>,bf,std::placeholders::_1);
af(b);
af = std::bind(castAndCall<decltype(cf),A>,cf,std::placeholders::_1);
af(c);
这篇关于我如何使用std :: function的多态性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!