简体   繁体   中英

using Boost `ptree.find` doesn't work as expected

I have

const boost::property_tree::ptree& v

and I want to get <xmlattr>.Value , if it exists, otherwise the value.

I tried this code:

if(v.find("<xmlattr>.Value") != v.not_found())
    value = v.get<std::string>("<xmlattr>.Value");
else
    value = v.get_value<std::string>();

However, it doesn't work as expected. Even if the value is there, find() returns not_found() .

This code works:

auto inValue = v.get_optional<std::string>("<xmlattr>.Value");
if(inValue.is_initialized())
    value = inValue.get();
else
    value = v.get_value<std::string>();

I guess I understood find() wrong. What exactly does it do? Is there another function I should use instead?

According to the documentation, find() (see here) finds a child with the given key (not path), or not_found() if there is none.

<xmlattr>.Value is a path (that works with get and get_optional ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM