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