10

A previous answer describes how to check if a key exists in a yaml node using YAML::Node::FindValue("parameter").

Unfortunately, I can't call this in the latest version (0.5.1):

 error: ‘class YAML::Node’ has no member named ‘FindValue’

Is this expected to work or is there an equivalent function which works in the latest version?

Community
  • 1
  • 1
paco_uk
  • 445
  • 1
  • 5
  • 15

1 Answers1

16

In the new API, you can just check:

if (node["parameter"]) {
  // ...
}

It may be convenient to define an object in the if (...) block:

if (YAML::Node parameter = node["parameter"]) {
  // process parameter
}
Jesse Beder
  • 33,081
  • 21
  • 109
  • 146
  • 2
    Great, thanks. Now you've spelled it out I see that's the first step in the tutorial. oops. – paco_uk Feb 24 '14 at 14:20
  • But doesn't `node["parameter"]` add an entry to the bottom-level containers potentially increasing the used memory? Reading the implementation of version `0.7.0` it seems to me that `emplace_back` is called on a couple of `std::map` members. I am not saying it matters/should be different, but I am not sure this is leaving node totally identical. – Axel Krypton Nov 21 '22 at 10:13
  • so would it be a bad idea to use a const reference like this: `void foo(const YAML::Node& node) { if (node) { ... } } foo(node["parameter"])` – Geronimo May 04 '23 at 17:49