Zozzzzz
Zozzzzz

Reputation: 193

Compare two variant with boost static_visitor

I started to use the boost library a few days ago so my question is maybe trivial. I want to compare two same type variants with a static_visitor. I tried the following, but it don't want to compile.

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;
}

Thank you for any help or suggestion!

Upvotes: 3

Views: 912

Answers (1)

Zozzzzz
Zozzzzz

Reputation: 193

llonesmiz told me the answer in a comment but it disappeared. If someone has a similar problem, it may helps: I had to handle every combination of int and double in different operators. The simpliest way to implement it was using templates, like this:

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

};

Upvotes: 1

Related Questions