Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 8th, 2012  |  syntax: None  |  size: 1.18 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Changing boost::variant underlying type from visitor
  2. struct sexpr {
  3.       typedef boost::variant<
  4.          nil,
  5.          int,
  6.          double,
  7.          symbol,
  8.          string,
  9.          boost::recursive_wrapper<list<sexpr> >
  10.       > node_type;
  11.  
  12.       node_type node;
  13.    };
  14.        
  15. struct push_back_visitor: public boost::static_visitor<void>
  16.    {
  17.       push_back_visitor(const sexpr &arg): arg_(arg) {}
  18.  
  19.       template <typename T>
  20.       void operator()(const T &value) const {
  21.          throw bad_visit();
  22.       }
  23.  
  24.       void operator()(nil &val) const {
  25.          // how to change the underlying type to list<sexpr> here?
  26.          // lst.push_back(arg_);
  27.       }
  28.  
  29.       void operator()(list<sexpr> &lst) const {
  30.          lst.push_back(arg_);
  31.       }
  32.  
  33.       sexpr arg_;
  34.    };
  35.        
  36. template<typename Variant, typename Visitor>
  37. struct carry_variant_visitor
  38.   : public boost::static_visitor<typename Visitor::result_type>
  39. {
  40.     carry_variant_visitor(Variant &variant, Visitor visitor):
  41.         variant_(variant), visitor_(visitor) { }
  42.     template<typename T>
  43.     typename Visitor::result_type operator()(T &t) const {
  44.         return visitor_(variant_, t);
  45.     }
  46.     Variant &variant_;
  47.     Visitor visitor_;
  48. }