yaml-cpp icon indicating copy to clipboard operation
yaml-cpp copied to clipboard

Tutorial: How to allow for optional fields?

Open mcandre opened this issue 4 years ago • 1 comments

The tutorial seems to require all fields to have values in the user's YAML file. However, many configurations permit omitting fields and falling back on defaults. Could we please expand the tutorial to show an example for this?

Update:

I now see the IsMap() is another option when validating and decoding YAML Nodes. Would be helpful to include an example using maps in the tutorial, and link to the API docs from the README.

mcandre avatar Sep 06 '21 18:09 mcandre

You can pass a default value in the as operator, eg.

int x = node["x"].as<int>(42);

Will set x to 42 if the property is missing.

SupSuper avatar Sep 09 '21 09:09 SupSuper