Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将两个变量与boost static\u进行比较_C++_Boost_Boost Variant - Fatal编程技术网

C++ 将两个变量与boost static\u进行比较

C++ 将两个变量与boost static\u进行比较,c++,boost,boost-variant,C++,Boost,Boost Variant,几天前我开始使用boost库,所以我的问题可能很简单。 我想将两个相同类型的变体与一个静态访问者进行比较。我尝试了以下方法,但它不想编译 struct compare:public boost::static_visitor<bool> { bool operator()(int& a, int& b) const { return a<b; } bool operator()(double& a, do

几天前我开始使用boost库,所以我的问题可能很简单。 我想将两个相同类型的变体与一个静态访问者进行比较。我尝试了以下方法,但它不想编译

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;
}
结构比较:public boost::static\u visitor { 布尔运算符()(int&a,int&b)常量 {
return allonesmiz在评论中告诉了我答案,但答案不见了。如果有人有类似问题,可能会有帮助: 我必须在不同的运算符中处理int和double的每个组合。实现它最简单的方法是使用模板,如下所示:

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

};
struct my\u less:boost::static\u visitor
{
模板
布尔运算符()(ta,ub)常数
{

返回Ank you,它解决了我的问题!@llonesmiz,将其添加为答案以获得积分并结束问题。
新布尔值
?如果编译成功,则由于指向布尔值转换的指针,它将始终返回
真值
。抱歉,我搞砸了…我使用布尔值时,它出现在我的最终代码中…我修复了它。