Skip to content

Defined in header <fkYAML/node.hpp>

fkyaml::basic_node::get_value

template <
    typename T, typename ValueType = detail::remove_cvref_t<T>,
    detail::enable_if_t<
        detail::conjunction<
            std::is_default_constructible<ValueType>, detail::has_from_node<basic_node, ValueType>>::value,
        int> = 0>
T get_value() const noexcept(
    noexcept(ConverterType<ValueType>::from_node(std::declval<const basic_node&>(), std::declval<ValueType&>())));

Explicit type conversion between the internally stored YAML node value and a compatible value which is copy-constructible and default-constructible.
The conversion relies on the node_value_converter::from_node.
This API makes a copy of the value.
If the copying costs a lot, or if you need an address of the original value, then it is more suitable to call get_value_ref instead.

Template Parameters

T
A compatible value type which might be cv-qualified or a reference type.
ValueType
A compatible value type.
This is, by default, a type of std::remove_cvref_t.
If fkYAML is compiled with C++11, C++14 or C++17, fkYAML uses its own implementation.

Return Value

A compatible native data value converted from the basic_node object.

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

int main() {
    // create a YAML node.
    fkyaml::node n = 123;
    fkyaml::node n2 = "foo";

    // get references to the value.
    auto int_val = n.get_value<int>();
    auto str_val = n2.get_value<std::string>();

    // print the values
    std::cout << int_val << std::endl;
    std::cout << str_val << std::endl;

    // specifying incompatible type throws an exception
    try {
        auto float_val = n2.get_value<float>();
    }
    catch (const fkyaml::exception& e) {
        std::cout << e.what() << std::endl;
    }

    return 0;
}

output:

123
foo
type_error: The target node value type is not float number type. type=string

See Also