Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had a similar problem and I used <code>make_recursive_variant</code> then I used <code>map&lt;Key,Value&gt;</code> for the variant instead of a Node class. And finally my node class became a utility function wrapping a <code>std::map</code> reference. Such as:</p> <pre><code>typedef boost::make_recursive_variant&lt; std::map&lt;std::string, boost::recursive_variant_&gt;, vector&lt;int&gt;, vector&lt;float&gt; &gt;::type data_t; // the printing visitor to ostream object class PrintData : public boost::static_visitor&lt;ostream&amp;&gt; { public: ostream* os; string prefix; /// keep a pointer to the ostream and keep /// a prefix string which will hold and "indent" /// which is something like " " for every level /// of recursion PrintData(ostream&amp; out_stream, const string&amp; prefix_str="") : os(&amp;out_stream) , prefix(prefix_str) {} /// recurse into the tree, adding indent characters to prefix ostream&amp; operator()(std::map&lt;std::string, data_t&gt;&amp; tree) const { *os &lt;&lt; endl; for (auto&amp; i : tree) { *os &lt;&lt; prefix &lt;&lt; i.first; //you may want to change prefix and use new PrintData instead of *this boost::apply_visitor(*this, i.second); } return *os; } /// actual data types that we want to print out template &lt;typename T&gt; ostream&amp; operator()(const vector&lt;T&gt;&amp; d) const { *os &lt;&lt; " (vector&lt;" &lt;&lt; type_demangle(typeid(T).name()) &lt;&lt; "&gt;):"; for (T i : d) { *os &lt;&lt; " " &lt;&lt; i; } *os &lt;&lt; endl; return *os; } }; class Node { public: //implicit ctor for data_t conversion Node(data_t&amp; data) : data(data) { } //... private: data_t&amp; data; }; </code></pre> <p>Edit: I forgot to mention that you dont need apply_visitor inside Node class anymore, but I am not sure about <code>initializer_list</code> ctor. It may not work with <code>data_t</code> types. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload