Skip to content

Defined in header <fkYAML/node.hpp>

fkyaml::basic_node::contains

template <
    typename KeyType, detail::enable_if_t<
                            detail::conjunction<
                                detail::negation<detail::is_basic_node<detail::remove_cvref_t<KeyType>>>,
                                detail::is_node_compatible_type<basic_node, detail::remove_cvref_t<KeyType>>>::value,
                            int> = 0>
bool contains(KeyType&& key) const; // (1) A compatible key object with basic_node type

template <
    typename KeyType, detail::enable_if_t<detail::is_basic_node<detail::remove_cvref_t<KeyType>>::value, int> = 0>
bool contains(KeyType&& key) const; // (2) A basic_node object

Checks if the YAML node has the given key.
If the node value is not a mapping, this API will throw an fkyaml::type_error.
The KeyType can be a compatible type with fkyaml::basic_node or a kind of fkyaml::basic_node template class.

Template Parameters

KeyType
A type which is compatible with or a kind of fkyaml::basic_node.

Parameters

key [in]
A key to the target value in the YAML mapping node value.

Return Value

true if the YAML node is a mapping and has the given key, false otherwise.

Example
#include <iomanip>
#include <iostream>
#include <fkYAML/node.hpp>

int main() {
    // create a YAML mapping node.
    fkyaml::node n = {{"foo", true}, {"bar", 123}};

    // check if the node has the following keys.
    std::cout << std::boolalpha;
    std::cout << n.contains("foo") << std::endl;
    std::cout << n.contains(fkyaml::node("baz")) << std::endl;

    // create a YAML node. (not mapping)
    fkyaml::node n2 = "qux";

    // check if the node has the following key.
    std::cout << std::boolalpha << n2.contains("qux") << std::endl;

    return 0;
}

output:

true
false
false

See Also