在 c++ 中包装来自不同库的自定义类型
Wrap custom types from different libraries in c++
当使用不同的库时,我总是发现每个库都有相同的"真实单词实体"的自定义类型。
假设我有一个使用 3 维点的项目,我只使用 OpenCv 和 PCL(点云库)的算法。我发现自己有这些类型
:- OpenCv Point3_
- 用于 PCL 的 PointXYZ
- Point3d 我的自定义类型
现在我有了为 Point3d 编写的算法,但我也想使用这些库中的算法。将大集合中的每个点从一种类型来回转换为另一种类型需要内存和时间。
围绕此进行某种抽象的最佳方法是什么?
你可以做这样的事情
template<class T>
struct Getter{
};
template<class T>
struct Getter<Point3_<T>>{
typedef T& type;
static type getx(Point3_<T>& p){
return p.x;
}
};
template<>
struct Getter<PointXYZ>{
typedef float& type;
static type getx(PointXYZ& p){
return p.x;
}
};
template <class T>
point_x(T& p) -> Getter<T>::type{
return Getter<T>::getx(p);
}
对 y 和 z 执行相同的操作然后修改您的算法以采用模板,而不是使用 p.x = ...用
getx(p) = ..
auto x = getx(p)
相关文章:
- 使用QJsEngine在Qt中注册自定义类型
- 当我使用自定义类型创建动态数组时,即使使用字符串,它似乎也不起作用
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- 如何使自定义类型在unordered_map中用作键
- Qt5 远程对象 + 自定义类型,但不在 POD 中
- 标准::原子中的自定义类型
- 如何使用自定义类型声明Arduino数组?
- 使用自定义访问者时具有自定义类型的提升变体失败(源自 boost::static_visitor)
- 您可以将binary_search应用于具有自定义类型的矢量吗?
- 在自定义类型图中重用 SWIG 映射
- 扩展自定义类型的spdlog
- 重载自定义类型的 std::to_string 和 std::to_chars?
- 自定义类型转换运算符在转发引用上调用时不起作用(当对象按值传递时有效)
- 初始化自定义类型构造函数的数组
- 如何使用 QListIterator 的声明自定义类型定义
- 向前删除标题文件中其他名称空间的自定义类型
- 如何为提升日志和自定义类型定义输出流运算符
- typedef 中的自定义类型为 Qt MetaType
- 无法专门化 std::hash 以unordered_map存储自定义类型
- 运算符/ STD :: Chrono ::持续时间和自定义类型与Clang