I have a recursive variant that models an S-expression:
struct sexpr {
typedef boost::variant<
nil,
int,
double,
symbol,
string,
boost::recursive_wrapper<list<sexpr> >
> node_type;
node_type node;
};
I'd like the empty list to always be represented by nil
(not list<sexpr>
). However, I'm stuck with the implementation of the push_back() visitor. When the underlying type is nil
, I'd like it to change that type to list<sexpr>
and to push back the supplied value:
struct push_back_visitor: public boost::static_visitor<void>
{
push_back_visitor(const sexpr &arg): arg_(arg) {}
template <typename T>
void operator()(const T &value) const {
throw bad_visit();
}
void operator()(nil &val) const {
// how to change the underlying type to list<sexpr> here?
// lst.push_back(arg_);
}
void operator()(list<sexpr> &lst) const {
lst.push_back(arg_);
}
sexpr arg_;
};
Any ideas?