I'm using boost-variant
throughout my projects and I'm considering it to be one the most useful and versatile tools of boost
.
But if it comes to complicated use of the visitor pattern with recursively nested variants the debugging is sometimes cumbersome.
Hence I decided to implement a reusable DebugVisitor
, that might be added to my existing visitors. It should be easily added/removed to my existing visitors in case a defect occurs.
Easily removable means, that it should be addable to any existing Visitor class, instead of modifying the places, where instances of the Visitor are being used.
I tried to find a solution, which fits my requirements. The following code compiles, but unfortunately it does not print the message.
Does anyone knows why?
#include <iostream>
#include <boost/variant.hpp>
#include <functional>
template<typename V> // V must have the boost::static_visitor Interface
struct DebugVisitor : public V {
template<typename U>
typename V::result_type operator()(const U& u) const {
std::cout << "Visiting Type:" << typeid(U).name() << " with Visitor: " << typeid(V).name() << std::endl;
return V::operator()(u);
}
};
struct AddVisitor : public DebugVisitor<boost::static_visitor<boost::variant<int, double>>> {
template<typename U>
result_type operator()(const U& u) const {
return u + 1.;
}
};
int main(int, char**) {
boost::variant<double, int> number{ 3.2 };
AddVisitor d;
auto incr=number.apply_visitor(d);
if (auto dValue = boost::get<double>(incr)) {
std::cout << "Increment: " << dValue << std::endl;
}
return 0;
}