Boost C++ Libraries: Ticket #8465: ptree get_child should not throw an exception on empty sets https://svn.boost.org/trac10/ticket/8465 <p> I feel there is an design issue with the ptree::get_child() method related to the choice of throwing an exception when reading an empty source. The typical use of get_child() employs of a for loop (or BOOST_FOREACH) which one expects to exhibit the standard behaviour of a container iterator: loop over contained items until finished, and if there are no items, then do nothing. Throwing an exception if no child exists does not conform to this expected behaviour (and was poorly documented as well...) and it also does not really comply with the concept of an exceptional condition (for cases where empty/optional sources are expected). I think it would be acceptable to throw an exception if the data content of the source was malformed, but an empty set could simply indicate optional data that is not present. </p> <p> I'm a fairly new user of Boost, so if there is an alternative method for iterating over child trees in a more iterator-compliant manner, I apologize for the disruption (although if this is the case, the documentation probably needs to be updated). </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8465 Trac 1.4.3 viboes Sat, 20 Apr 2013 10:40:17 GMT component changed; owner set https://svn.boost.org/trac10/ticket/8465#comment:1 https://svn.boost.org/trac10/ticket/8465#comment:1 <ul> <li><strong>owner</strong> set to <span class="trac-author">Sebastian Redl</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">property_tree</span> </li> </ul> Ticket Sebastian Redl Fri, 23 Jan 2015 11:03:43 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/8465#comment:2 https://svn.boost.org/trac10/ticket/8465#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> Can you explain what your typical get_child loop looks like? </p> <p> If it is </p> <blockquote> <p> for (const auto&amp; c : tree.get_child("path.to.child")) </p> </blockquote> <p> then I recommend you use tree.get_child("path.to.child", ptree()) instead to avoid the exception and simply get no loop iterations. </p> Ticket anonymous Fri, 23 Jan 2015 14:09:18 GMT <link>https://svn.boost.org/trac10/ticket/8465#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8465#comment:3</guid> <description> <p> Yes, the pattern you show is typical, and your solution is valid; however, this was not clear (and still isn't) from the documentation of ptree. Perhaps I'm looking in the wrong place for function-level documentation? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Mon, 30 May 2016 16:41:39 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8465#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8465#comment:4</guid> <description> <p> I agree with Dale and additionally the workaround doesn't work anymore for me. Instead I have to do </p> <pre class="wiki">ptree empty_tree; for (const auto&amp; c : tree.get_child("path.to.child", empty_tree)) {} </pre><p> ugly:( </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Sun, 04 Sep 2016 19:37:47 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8465#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8465#comment:5</guid> <description> <p> it seems this issue is still present </p> </description> <category>Ticket</category> </item> </channel> </rss>