我目前正在编写一个有时会与 OpenCV 一起使用的库。因为 OpenCV 定义了一个 Point_ 类,该类常用于我的库将在其中使用的某些上下文中,所以我想添加选项以将 Point_s 作为参数传递给我的一些函数。也就是说,OpenCV 是一个非常繁重的库,我非常不希望仅仅为了访问它的 Point_ 类而依赖它。
定义我自己的 Point_ 相同 Point_ 类会导致预期的多重定义错误。
我考虑过使用预处理器宏来检查包含 Point_ 的 OpenCV header 是否已经被包含,并且只在没有被包含的情况下定义它,但我担心如果我的库 header 首先被包含,多重定义错误将返回,这将使我的图书馆难以为我以外的任何人使用。
有没有一种方法可以提供一个定义,该定义仅在其他任何地方都没有定义时才使用,并且/或者如果某个定义出现在其他地方则将被覆盖?
最佳答案
您可以根据您的 点类定义您的 库,并有选择地为OpenCV 生成转换
库类型(如果存在)。像这样:
#ifdef HAVE_OPENCV
#include <opencv2/opencv.hpp>
#endif
struct my_point
{
double x;
double y;
#ifdef HAVE_OPENCV
my_point(cv::Point2d p): x(p.x), y(p.y) {}
operator cv::Point2d() const { return {x, y}; }
#endif
};
my_point my_function(my_point p)
{
return p;
}
int main()
{
cv::Point2d p;
// automatic conversions happen between OpenCV version
// and your library's version
cv::Point2d q = my_function(p);
}
因为转换运算符是微不足道的内联函数,编译器会将它们完全优化掉,让代码就像根本没有发生任何转换一样。
您可以选择(最好是 imo)进行转换显式,这可能会使代码更安全:
struct my_point
{
double x;
double y;
#ifdef HAVE_OPENCV
// make these explicit
explicit my_point(cv::Point2d p): x(p.x), y(p.y) {}
explicit operator cv::Point2d() const { return {x, y}; }
#endif
};
my_point my_function(my_point p)
{
return p;
}
int main()
{
cv::Point2d p;
// Now the library user needs to explicitly ask for
// the conversions to take place
cv::Point2d q = cv::Point2d(my_function(my_point(p)));
}
关于c++ - 如何在不添加对库的依赖的情况下添加与库结构的兼容性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51308746/