Where is boost_tree :: empty_ptree?

I am using boots property_tree library. I am looking for a way to get a child of a node from an object ptree, but return empty ptreeif that fails. I came across a good example in property_tree / examples / empty_ptree_trick.cpp:

void process_settings(const std::string &filename)
{
    ptree pt;
    read_info(filename, pt);    
    const ptree &settings = pt.get_child("settings", empty_ptree<ptree>());
    std::cout << "\n    Processing " << filename << std::endl;
    std::cout << "        Setting 1 is " << settings.get("setting1", 0) << std::endl;
    std::cout << "        Setting 2 is " << settings.get("setting2", 0.0) << std::endl;
    std::cout << "        Setting 3 is " << settings.get("setting3", "default") <<     std::endl;
}

which does exactly what I need. The problem is that the compiler complains that the function is empty_ptree()not a member boost:property_tree. Any ideas where empty_ptree()?

I am using boost 1.44 on VS2010.

+5
source share
1 answer

I just blew up all day trying to answer this question!

. -, , , . ptree.

using namespace boost::property_tree;

ptree r_pt;
ptree *c_pt;

read_xml( "file.xml" , r_pt);

try {
    c_pt = &(r_pt.get_child( "example" ));
}
catch (ptree_bad_path) {
    c_pt = &(r_pt.put_child( "example", ptree() ));
}

std::cout << "Setting 1 is " << c_pt.get("setting1", 0) << std::endl;

, , , boost:: optional type. .

empty_ptree < > .

template<class Ptree>
    inline const Ptree &empty_ptree()
    {
        static Ptree pt;
        return pt;
    }

, , empty_ptree_trick.cpp, , , .

+1

All Articles