where is boost property_tree::empty_ptree?

拟墨画扇 提交于 2019-12-05 11:22:54

I have just blown a full day trying to answer that question!

This was my solution. Firstly I used pointers, and not references as you have to initialize them immediately. Then I just caught the exception and added a new 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;

From what I could pick up they expect us to use the boost::optional type. But I'm just a beginner..

EDIT I just found the implementation of empty_ptree<>.

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

I think you can just add this to your code and use it as described in the empty_ptree_trick.cpp, but I am sticking with my solution for now untill I find out how its actually supposed to be done.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!