将两个变体与Boost static_visitor进行比较

Compare two variant with boost static_visitor

本文关键字:static Boost visitor 比较 两个      更新时间:2023-10-16

我几天前开始使用boost库,所以我的问题可能很琐碎。我想将两个相同类型的变体与static_visitor进行比较。我尝试了以下内容,但它不想编译。

struct compare:public boost::static_visitor<bool>
{
    bool operator()(int& a, int& b) const
    {
        return a<b;
    }
    bool operator()(double& a, double& b) const
    {
        return a<b;
    }
};
int main()
{
    boost::variant<double, int > v1, v2;
    v1 = 3.14;
    v2 = 5.25;
    compare vis;
    bool b = boost::apply_visitor(vis, v1,v2);
    cout<<b;
    return 0;
}

感谢您的任何帮助或建议!

llonesmiz在评论中告诉我答案,但它消失了。如果某人有类似的问题,它可能会有所帮助:我必须处理不同运算符中INT和双倍组合的所有组合。实施它的最简单方法是使用模板,例如:

struct my_less : boost::static_visitor<bool>
{
   template<typename T, typename U>
   bool operator()(T a, U b) const
   {
       return a<b;
   }   
};