title: Boost::property_tree: Using std::vector<> in XML parser to store multiple values in one key
body: <p>My question is related to this question: <a href="https://stackoverflow.com/questions/13072704/boost-property-tree-multiple-values-per-key">Boost property_tree: multiple values per key</a> and to a question following that question: <a href="https://stackoverflow.com/questions/29801003/boost-property-tree-multiple-values-per-key-on-a-template-class">Boost property_tree: multiple values per key, on a template class</a>.</p>
  
  <p>I am trying to parse an XML file in which multiple values are listed at a single key value using <code>std::vector&lt;&gt;</code>. The following code is what I have implemented so far:</p>
  
  <pre><code>#include &lt;boost/optional.hpp&gt;
  #include &lt;boost/property_tree/xml_parser.hpp&gt;
  
  namespace boost { namespace property_tree
  {
  
  template&lt;typename type&gt;
  struct vector_xml_translator
  {
      boost::optional&lt;std::vector&lt;type&gt; &gt; get_value(const std::string&amp; str)
      {
          if (!str.empty())
          {
              std::vector&lt;type&gt; values;
              std::stringstream ss(str);
  
              while (ss)
              {
                  type temp_value;
                  ss &gt;&gt; temp_value;
                  values.push_back(temp_value);
              }
  
              return boost::optional&lt;std::vector&lt;type&gt; &gt;(values);
          }
          else
          {
              return boost::optional&lt;std::vector&lt;type&gt; &gt;(boost::none);
          }
      }
  
      boost::optional&lt;std::string&gt; put_value(const std::vector&lt;type&gt;&amp; b)
      {
          std::stringstream ss;
          for (unsigned int i = 0; i &lt; b.size(); i++)
          {
              ss &lt;&lt; b[i];
              if (i != b.size()-1)
              {
                  ss &lt;&lt; " ";
              }
          }
          return boost::optional&lt;std::string&gt;(ss.str());
      }
  };
  
  template&lt;typename ch, typename traits, typename alloc, typename data_type&gt;
  struct translator_between&lt;std::basic_string&lt;ch, traits, alloc&gt;, std::vector&lt;data_type&gt; &gt;
  {
      typedef vector_xml_translator&lt;data_type&gt; type;
  };
  
  } // namespace property_tree
  } // namespace boost
  </code></pre>
  
  <p>A minimal example to test this code is as follows:</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;boost/property_tree/ptree.hpp&gt;
  #include &lt;XML_Vector_Translator.hpp&gt;
  
  
  int main()
  {
      using boost::property_tree::ptree;
  
      std::vector&lt;double&gt; test_vector;
  
      test_vector.push_back(1);
      test_vector.push_back(6);
      test_vector.push_back(3);
  
  
      ptree pt;
      pt.add("base", test_vector);
  
      std::ofstream os("test_file.xml");
  
      write_xml(os, pt, boost::property_tree::xml_writer_settings&lt;std::string&gt;(' ', 2));
  
      std::ifstream is("test_file.xml");
  
      ptree pt_2;
      read_xml(is, pt_2);
  
      std::vector&lt;int&gt; test_vector_2;
  
      test_vector_2 = pt_2.get&lt;std::vector&lt;int&gt; &gt;("base");
  
      for (unsigned int i = 0; i &lt; test_vector_2.size(); i++)
      {
          std::cout &lt;&lt; test_vector_2[i] &lt;&lt; std::endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>When I run this code I get a number of errors, which lead me to believe that the registration of the translator structure is not right. Does anybody have an idea of how to solve this issue and/or improve this code?</p>