Question

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.

Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top