将 boost::function 与指向派生类的共享指针的参数一起使用
Using boost::function with a parameter to shared pointer to derived class
在 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
,这要求类A
和B
是多态类型。
住在科里鲁
#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%的代码减少。
相关文章:
- 如何从具有移动语义的类对象中生成共享指针
- 使用共享指针的函数调用,其对象应为 const
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 如何访问由共享指针保存的类方法?
- 从矢量或地图中删除共享指针
- 使用共享指针时,从共享指针本身释放内存的机制是什么
- 使用共享指针实现复制 c'tor?
- C++/QT:使用指向私有成员的常量指针作为只读数据共享
- 共享指针继承,而不先显式强制转换
- 如何检查类中共享指针的有效性?
- 共享 C++ 的数据成员指针
- std::排序在共享指针的向量上
- 将相同共享指针的副本存储在不同的向量中是否是一种好的做法?
- 在什么情况下,需要共享智能指针而无法使用唯一指针?
- 嵌套类、继承和C++中的共享指针
- <Base> <Derived> 具有相同原始指针共享引用的 shared_ptr 和 shared_ptr 实例是否计数?
- 重载流<<运算符,用于指针/共享指针和其他类型的
- RAW指针共享_ptr
- 指针共享习语
- 将memmove函数与临时指针共享相同的地址