Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent calls to get_child with temporary default values #115

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/boost/property_tree/ptree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ namespace boost { namespace property_tree
const self_type &get_child(const path_type &path,
const self_type &default_value) const;

/** Prevents calls to get_child with temporary default values */
void get_child(const path_type &path,
const self_type &&default_value) const = delete;

/** Get the child at the given path, or return boost::null. */
optional<self_type &> get_child_optional(const path_type &path);

Expand Down
6 changes: 6 additions & 0 deletions test/test_property_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// ----------------------------------------------------------------------------
#include "test_utils.hpp"
#include <boost/any.hpp>
#include <boost/mp11.hpp>
#include <boost/range.hpp>
#include <list>
#include <cmath>
Expand Down Expand Up @@ -67,6 +68,11 @@ struct any_translator
}
};

// Checks the validity of calling get_child with a default value type
template<class P, class C, class D>
using get_child_accepts_default_of_type = decltype(
std::declval<P>().get_child(std::declval<const C*>(), std::declval<D>()));

namespace boost { namespace property_tree {
template <typename E>
struct translator_between<boost::any, E>
Expand Down
29 changes: 21 additions & 8 deletions test/test_property_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,14 +820,27 @@ void test_get_child_put_child(PTREE *)
BOOST_TEST(cpt2.get_child(T("k2.k")) == pt);

// Do correct extractions via get_child (default value version)
BOOST_TEST(pt1.get_child(T("k1"), PTREE(T("def"))) != PTREE(T("def")));
BOOST_TEST(pt1.get_child(T("k2.k"), PTREE(T("def"))) != PTREE(T("def")));
BOOST_TEST(pt2.get_child(T("k1"), PTREE(T("def"))) == pt);
BOOST_TEST(pt2.get_child(T("k2.k"), PTREE(T("def"))) == pt);
BOOST_TEST(cpt1.get_child(T("k1"), PTREE(T("def"))) != PTREE(T("def")));
BOOST_TEST(cpt1.get_child(T("k2.k"), PTREE(T("def"))) != PTREE(T("def")));
BOOST_TEST(cpt2.get_child(T("k1"), PTREE(T("def"))) == pt);
BOOST_TEST(cpt2.get_child(T("k2.k"), PTREE(T("def"))) == pt);
PTREE dflt(T("def"));
BOOST_TEST(pt1.get_child(T("k1"), dflt) != dflt);
BOOST_TEST(pt1.get_child(T("k2.k"), dflt) != dflt);
BOOST_TEST(pt2.get_child(T("k1"), dflt) == pt);
BOOST_TEST(pt2.get_child(T("k2.k"), dflt) == pt);
BOOST_TEST(cpt1.get_child(T("k1"), dflt) != dflt);
BOOST_TEST(cpt1.get_child(T("k2.k"), dflt) != dflt);
BOOST_TEST(cpt2.get_child(T("k1"), dflt) == pt);
BOOST_TEST(cpt2.get_child(T("k2.k"), dflt) == pt);

// Ensure that get_child does not compile when using temporaries as the default value.
BOOST_TEST((!boost::mp11::mp_valid<get_child_accepts_default_of_type, const PTREE&, CHTYPE, PTREE>::value));
BOOST_TEST((!boost::mp11::mp_valid<get_child_accepts_default_of_type, const PTREE&, CHTYPE, const PTREE>::value));
BOOST_TEST((!boost::mp11::mp_valid<get_child_accepts_default_of_type, PTREE&, CHTYPE, PTREE>::value));
BOOST_TEST((!boost::mp11::mp_valid<get_child_accepts_default_of_type, PTREE&, CHTYPE, const PTREE>::value));

// Test get_child_accepts_default_of_type itself
BOOST_TEST((boost::mp11::mp_valid<get_child_accepts_default_of_type, const PTREE&, CHTYPE, PTREE&>::value));
BOOST_TEST((boost::mp11::mp_valid<get_child_accepts_default_of_type, const PTREE&, CHTYPE, const PTREE&>::value));
BOOST_TEST((boost::mp11::mp_valid<get_child_accepts_default_of_type, PTREE&, CHTYPE, PTREE&>::value));
BOOST_TEST((boost::mp11::mp_valid<get_child_accepts_default_of_type, PTREE&, CHTYPE, const PTREE&>::value));

// Do correct extractions via get_child (optional version)
boost::optional<PTREE &> opt;
Expand Down