I'm using boost::variant
quite often in my projects. My colleagues now came up with the idea to pass around instances of specific boost::static_visitor<int>
in order to customize the type of visitation. She had some code like the following:
#include <boost/variant.hpp>
#include <iostream>
typedef boost::variant<int, std::string> TVar;
struct Visitor1 : public boost::static_visitor<int> {
template<typename T>
result_type operator()(const T&) {
return 42;
}
};
struct Visitor2 : public boost::static_visitor<int> {
template<typename T>
result_type operator()(const T&) {
return 21;
}
};
int adder(const boost::static_visitor<int>& visitor, const TVar& visitable) {
return visitable.apply_visitor(visitor) + 1;
}
int main(int argc, char **args) {
Visitor1 v1;
Visitor2 v2;
TVar x;
std::cout << adder(v1, x) << std::endl;
std::cout << adder(v2, x) << std::endl;
}
It looks perfectly sound to me, but it doesn't compile. My compiler says that expression will not yield a function that takes one 1 argument
somewhere insider .
What are we doing wrong?