如何在不增加对库的依赖的情况下添加与库结构的兼容性?

Dag*_*cia 5 c++ dependencies shared-libraries multiple-definition-error

我目前正在编写一个有时与OpenCV一起使用的库.因为OpenCV定义了一个在我的库中将使用的一些上下文中常用的Point_类,所以我想添加选项以将Point_s作为参数传递给我的一些函数.也就是说,OpenCV是一个非常繁重的库,我非常希望不依赖它只是为了访问它的Point_类.

定义我自己的Point_相同的Point_类会导致预期的多重定义错误.

我考虑使用预处理器宏来检查是否已经包含了包含Point_的OpenCV头,并且仅在尚未定义的情况下定义它,但是我担心如果首先包含我的库头,则多重定义错误将返回这将使我的图书馆难以用于除我之外的任何人.

有没有办法提供只有在其他地方没有定义时才会使用的定义,和/或如果其他地方出现在其他地方会被覆盖?

Gal*_*lik 3

您可以做的是根据点类定义您的,并可选择生成库类型的转换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);
}
Run Code Online (Sandbox Code Playgroud)

由于转换运算符是微不足道的内联函数,因此编译器将完全优化它们,使代码看起来就像根本没有发生转换一样。

或者(最好是在我看来)您可以显式进行转换,这可能会使代码更安全:

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)));
}
Run Code Online (Sandbox Code Playgroud)