在Ubuntu 16.04上使用C ++和g ++ 5.4.0。
我有一个类A和一个从类A派生的类B.函数f1将一个指向类A的共享指针作为参数。函数f2将一个指向B类的共享指针作为参数,并返回与f1相同的类型。使用boost :: function,另一个函数F将f1等函数作为参数。代码如下所示:
result_t f1 ( const boost::shared_ptr<A> a );
result_t f2 ( const boost::shared_ptr<B> b );
typedef boost::function < result_t (const boost::shared_ptr<A>&) > f1_t;
void F ( const f1_t f );
使用f1作为参数调用F可以正常工作。我现在想要调用F但是以f2作为参数。我收到以下错误:
error: invalid initialization of reference of type const boost::shared_ptr<B>& from expression of type const boost::shared_ptr<A>
return f(BOOST_FUNCTION_ARGS);
实际上并不需要F来解决这个错误:
f1_t f = f2;
给出了同样的错误。
答案 0 :(得分:1)
函数原型没有协方差。不同的签名就是:不同的类型。
在这种情况下,您需要使用转换包装器包装该函数。
让我们创建一些设施定义:
using result_t = int;
struct A { };
struct B : A { };
typedef boost::shared_ptr<A> APtr;
typedef boost::shared_ptr<B> BPtr;
result_t f1(APtr) { return 1; }
result_t f2(BPtr) { return 2; }
typedef boost::function <result_t(APtr const&)> funOfA;
typedef boost::function <result_t(BPtr const&)> funOfB;
现在将funOfB
包裹为funOfA
将如下所示:
funOfA wrapFunOfB(const funOfB f) {
struct {
funOfB _f;
result_t operator()(APtr const& a) const {
return _f(boost::static_pointer_cast<B>(a));
}
} wrap { f };
return wrap;
}
现在你可以轻松地写下:
int main() {
F(f1);
F(wrapFunOfB(f2));
}
<强> Live On Coliru 强>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/function.hpp>
#include <iostream>
typedef int result_t;
struct A { int i; };
struct B : A { int j; };
typedef boost::shared_ptr<A> APtr;
typedef boost::shared_ptr<B> BPtr;
result_t f1(APtr) { return 1; }
result_t f2(BPtr) { return 2; }
typedef boost::function <result_t(APtr const&)> funOfA;
typedef boost::function <result_t(BPtr const&)> funOfB;
struct Wrapper {
typedef result_t result_type;
funOfB _f;
result_t operator()(APtr const& a) {
return _f(boost::static_pointer_cast<B>(a));
}
};
funOfA wrapFunOfB(const funOfB f) {
Wrapper wrap = { f };
return wrap;
}
void F(const funOfA f) {
APtr a = boost::make_shared<A>();
APtr b = boost::make_shared<B>();
//std::cout << "f(a): " << f(a) << "\n"; // UNDEFINED BEHAVIOUR if f wraps a funOfB
std::cout << "f(b): " << f(b) << "\n";
}
int main() {
F(f1);
F(wrapFunOfB(f2));
}
打印
f(b): 1
f(b): 2
dynamic_pointer_cast<>
如果F
实际调用的对象<{1}} B
将调用Undefined Behaviour static_cast<>
}。
如果您想要避免这种情况,请使用dynamic_pointer_cast
,这要求类A
和B
为多态类型。
<强> Live On Coliru 强>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/function.hpp>
#include <iostream>
typedef int result_t;
struct A { int i; virtual ~A() {} };
struct B : A { int j; };
typedef boost::shared_ptr<A> APtr;
typedef boost::shared_ptr<B> BPtr;
result_t f1(APtr a) { return a?1 : 0; }
result_t f2(BPtr b) { return b?2 : -99; }
typedef boost::function <result_t(APtr const&)> funOfA;
typedef boost::function <result_t(BPtr const&)> funOfB;
struct Wrapper {
typedef result_t result_type;
funOfB _f;
result_t operator()(APtr const& a) {
return _f(boost::dynamic_pointer_cast<B>(a));
}
};
funOfA wrapFunOfB(const funOfB f) {
Wrapper wrap = { f };
return wrap;
}
void F(const funOfA f) {
APtr a = boost::make_shared<A>();
APtr b = boost::make_shared<B>();
std::cout << "f(a): " << f(a) << "\n";
std::cout << "f(b): " << f(b) << "\n";
}
int main() {
F(f1);
F(wrapFunOfB(f2));
}
打印
f(a): 1
f(b): 1
f(a): -99
f(b): 2
这里的事情变得更加优雅。值得注意的是,Wrapper类可以是本地的,也可以是匿名的:
funOfA wrapFunOfB(const funOfB f) {
struct {
typedef result_t result_type;
funOfB _f;
result_t operator()(APtr const& a) {
return _f(std::dynamic_pointer_cast<B>(a));
}
} wrap { f };
return wrap;
}
下一级:改为使用lambda:
funOfA wrapFunOfB(const funOfB f) {
return [f](APtr const& a) { return f(std::dynamic_pointer_cast<B>(a)); };
}
<强> Live On Coliru 强>
#include <memory>
#include <functional>
#include <iostream>
typedef int result_t;
struct A { int i; virtual ~A() {} };
struct B : A { int j; };
typedef std::shared_ptr<A> APtr;
typedef std::shared_ptr<B> BPtr;
result_t f1(APtr a) { return a?1 : 0; }
result_t f2(BPtr b) { return b?2 : -99; }
typedef std::function<result_t(APtr const&)> funOfA;
typedef std::function<result_t(BPtr const&)> funOfB;
funOfA wrapFunOfB(const funOfB f) {
return [f](APtr const& a) { return f(std::dynamic_pointer_cast<B>(a)); };
}
void F(const funOfA f) {
APtr a = std::make_shared<A>();
APtr b = std::make_shared<B>();
std::cout << "f(a): " << f(a) << "\n";
std::cout << "f(b): " << f(b) << "\n";
}
int main() {
F(f1);
F(wrapFunOfB(f2));
}
这减少了25%的代码。