Question

I am getting the following compiler error:

/usr/include/boost/variant/variant.hpp:832:32: error: no match for call to ‘(const StartsWith) (bool&)’

for the following code. Does anybody know why?

#include "boost/variant/variant.hpp"
#include "boost/variant/apply_visitor.hpp"

using namespace std;
using namespace boost;

typedef variant<bool, int, string, const char*> MyVariant;

class StartsWith
    : public boost::static_visitor<bool>
{
public:
    string mPrefix;
    bool operator()(string &other) const
    {
        return other.compare(0, mPrefix.length(), mPrefix);
    }
    StartsWith(string const& prefix):mPrefix(prefix){}
};

int main(int argc, char **argv) 
{
    MyVariant s1 = "hello world!";
    apply_visitor(StartsWith("hel"), s1); // << compiler error
    return 0;
}
Was it helpful?

Solution

You have to provide operators for every type declared in MyVariant.

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