#8465 closed Bugs (invalid)
ptree get_child should not throw an exception on empty sets
Reported by: | Owned by: | Sebastian Redl | |
---|---|---|---|
Milestone: | To Be Determined | Component: | property_tree |
Version: | Boost 1.53.0 | Severity: | Problem |
Keywords: | Cc: |
Description
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.
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).
Change History (5)
comment:1 by , 10 years ago
Component: | None → property_tree |
---|---|
Owner: | set to |
comment:2 by , 8 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:3 by , 8 years ago
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?
comment:4 by , 6 years ago
I agree with Dale and additionally the workaround doesn't work anymore for me. Instead I have to do
ptree empty_tree; for (const auto& c : tree.get_child("path.to.child", empty_tree)) {}
ugly:(
Can you explain what your typical get_child loop looks like?
If it is
then I recommend you use tree.get_child("path.to.child", ptree()) instead to avoid the exception and simply get no loop iterations.