title: Conversion of variant, vector<variant> and vector<vector<variant>> to the equivalent type of my choice
body: <p>My question concerns the mix of <code>boost::variant</code> conversions with <code>std::vector</code> in C++. In my project, I use variants as output of SQL. I will always just use 1 type of variant. What I am trying to do is to have an easy conversion from variant, vector of variant or 2xvector of variant to the equivalent type of my choice. Naively, I need something along the lines of:</p>
  
  <pre><code>std::vector &lt; int &gt; my_variable = convert(some_variant_vector)
  </code></pre>
  
  <p>First here is my setup: </p>
  
  <pre><code>#include "boost\variant.hpp"
  #include "boost\date_time\gregorian\gregorian.hpp"
  typedef boost::variant&lt; int, std::string, boost::gregorian::date&gt; _var;
  typedef std::vector&lt;_var&gt; _vec_var; 
  typedef std::vector&lt;_vec_var&gt; _vec2_var;
  </code></pre>
  
  <p>I would like to have a simple way to convert a <code>_var</code> (or <code>_vec_var</code> or <code>_vec2_var</code>) into a int/string/date depending on my needs. From the following <a href="https://stackoverflow.com/questions/15548125/convert-a-stdvector-of-a-boostvariant-type">post</a> I know that my answer should look something like this: </p>
  
  <pre><code>template&lt;typename T&gt;
  struct converter_visitor : public boost::static_visitor&lt;T&gt;
  {
      const _var &amp;converter;
      converter_visitor(const _var &amp;r) : converter(r) {}
      T operator()(const _var &amp;) const{
          return boost::get&lt;_var&gt;(converter);
      }
  
      const _vec_var &amp;v_converter;            // case of vector&lt;&gt;
      converter_visitor(const _vec_var &amp;r) : v_converter(r) {}
      T operator()(const _vec_var &amp;) const{
          T ans;
          ans.reserve(_cont.size());
          for (int i = 0; i &lt; _cont.size(); ++i)
              ans.push_back(boost::get&lt;T&gt;(v_converter[i]));
          return ans;
      }
  
      const _vec2_var &amp; v2_converter;         // case of vector&lt;vector&gt;
      converter_visitor(const _vec2_var &amp;r) : v2_converter(r) {}
      T operator()(const _vec2_var &amp;) const {
          T ans;
          ans.reserve(v2_converter.size(), v2_converter[0].size());
          for (int i = 0; i &lt; _cont.size(); ++i)
          {
              for (size_t j = 0; j &lt; v2_converter[0].size(); j++)
              {
                  ans.push_back(boost::get&lt;T&gt;(v2_converter[i][j]));
              }
          }
          return ans;
      }
  };
  
  int main()
  {
      _var variable = 1;
      int integer_conversion;
      boost::apply_visitor(converter_visitor(test), integer_conversion);
      return 0;
  }
  </code></pre>
  
  <p>Unfortunately I have been stuck in a while with this as it is not working as intended and does not even compile. </p>