Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost apply::visitor -> discards qualifiers

I have made a program using boost::variant that somehow isn't const correct.

error: passing ‘const CompareTitle’ as ‘this’ argument of ‘bool CompareTitle::operator()(const T1&, const T2&) [with T1 = TestSeizoen, T2 = TestSeizoen]’ discards qualifiers [-fpermissive]

same error for [T1=TestFilm, T2 = TestSeizoen] and so on.

This is the code:

#include <iostream>
#include <vector>
#include "boost/variant.hpp"

using namespace std;

class TestFilm{
private:
   string titel_;
public:
   TestFilm(const string& titel): titel_(titel){};
   const string titel() const {return titel_;};
};

class TestSeizoen{
private:
    string titel_;
public:
    TestSeizoen(const string& titel): titel_(titel){};
    const string titel() const {return titel_;};
};


struct CompareTitle: boost::static_visitor<bool>{
  template <typename T1, typename T2>
  bool operator() (const T1& t1 , const T2& t2){
     return t1.titel() == t2.titel();
  }
};


int main() {
   typedef boost::variant<TestFilm,TestSeizoen> var;
   vector <var> vec;
       TestFilm film1("titel1");
   vec.push_back(film1);
   TestSeizoen seizoen1("titel2");
   vec.push_back(seizoen1);
   vector<var>::iterator it;
   bool compare = boost::apply_visitor(CompareTitle(),*vec.begin(),*(vec.begin()+1));
   return 0;
}

I tried to make operator() a const member function but this didn't solve the problem. Can anybody help? I can provide more information if needed. Thanks in advance.

like image 546
Hannah P Avatar asked Jun 28 '26 17:06

Hannah P


1 Answers

I think you need to promise that operator() won't change the struct members, i.e.:

bool operator() (const T1& t1 , const T2& t2) const {
...
}

This compiles for me (with GCC 4.4.5).

like image 97
ales_t Avatar answered Jun 30 '26 19:06

ales_t



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!