将 boost::function 与指向派生类的共享指针的参数一起使用

Using boost::function with a parameter to shared pointer to derived class

本文关键字:指针 共享 参数 一起 派生 function boost      更新时间:2023-10-16

在 Ubuntu 16.04 上使用带有 g++ 5.4.0 的 C++。

我有一个类 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;

给出相同的错误。

函数原型没有协方差。不同的签名就是:不同的类型。

在这种情况下,您需要使用转换包装器包装函数。

让我们创建一些设施定义:

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));
}

简单C++03 演示

住在科里鲁

#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实际上调用了实际上不是B类型的对象的参数,则该static_cast<>将调用未定义的行为。

如果要防止这种情况,请使用dynamic_pointer_cast,这要求类AB多态类型

住在科里鲁

#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

C++11 版本

这里的事情变得更加优雅。值得注意的是,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)); };
}

住在科里鲁

#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%的代码减少。