There is no need to create a visitor for assigning values to a boost::variant
. As shown in the Basic Usage section of the tutorial, you just assign the value:
TreeVariant tree;
Tree<std::string> stringTree;
Tree<int> intTree;
tree = stringTree;
tree = intTree;
As for calling member functions, you should use a visitor:
class TreeVisitor : public boost::static_visitor<>
{
public:
void operator()(Tree<std::string>& tree) const
{
// Do something with the string tree
}
void operator()(Tree<int>& tree) const
{
// Do something with the int tree
}
};
boost::apply_visitor(TreeVisitor(), tree);
You can also return values from a static_visitor
, like so:
class TreeVisitor : public boost::static_visitor<bool>
{
public:
bool operator()(Tree<std::string>& tree) const
{
// Do something with the string tree
return true;
}
bool operator()(Tree<int>& tree) const
{
// Do something with the int tree
return false;
}
};
bool result = boost::apply_visitor(TreeVisitor(), tree);