Skip to content

Defined in header <fkYAML/node.hpp>

fkyaml::basic_node::get_value_ref

template <typename ReferenceType, detail::enable_if_t<std::is_reference<ReferenceType>::value, int> = 0>
ReferenceType get_value_ref() const noexcept(
    noexcept(ConverterType<ValueType>::from_node(std::declval<const basic_node&>(), std::declval<ValueType&>())));

template <
    typename ReferenceType,
    detail::enable_if_t<
        detail::conjunction<
            std::is_reference<ReferenceType>, std::is_const<detail::remove_reference_t<ReferenceType>>>::value,
        int> = 0>
ReferenceType get_value_ref() const;

Explicit reference access to the internally stored YAML node value.
This API makes no copies.

Template Parameters

ReferenceType
reference type to the target YAML node value.
This must be a (const) reference type to sequence_type, mapping_type, boolean_type, integer_type, float_number_type or string_type.

Return Value

Reference to the internally stored YAML node value if the requested reference type fits to the YAML node value.
A fkyaml::exception would be thrown otherwise.

Note

If this API throws an exception, the internally stored YAML node value stays intact.

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

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

    // get references to the value.
    auto ref = n.get_value_ref<fkyaml::node::integer_type&>();
    auto cref = n.get_value_ref<const fkyaml::node::integer_type&>();

    // print the referenced values
    std::cout << ref << std::endl;
    std::cout << cref << std::endl;

    // specifying incompatible reference type throws an exception
    try {
        auto iref = n.get_value_ref<fkyaml::node::mapping_type&>();
    }
    catch (const fkyaml::exception& e) {
        std::cout << e.what() << std::endl;
    }

    return 0;
}

output:

123
123
type_error: The node value is not a mapping. type=INTEGER

See Also