Skip to content

Defined in header <fkYAML/node_value_converter.hpp>

fkyaml::node_value_converter::from_node

template <typename BasicNodeType, typename TargetType = ValueType>
static auto to_node(BasicNodeType& n, TargetType&& val) noexcept(
    noexcept(::fkyaml::to_node(n, std::forward<TargetType>(val))))
    -> decltype(::fkyaml::to_node(n, std::forward<TargetType>(val)))

Converts a value to a basic_node object.
This function is usually called by the constructors of the basic_node class.

Customization for non-standard types

This function can be used for user-defined types by implementing (partial) specialization for to_node() function which is called internally by this function.
Note that the specialization must be implemented in the same namespace as the user-defined types so that the specialization can successfully be found by ADL (Argument-Dependent Lookup).
See the example below to get the feel of how it can be achieved.

Template Parameters

BasicNodeType
A basic_node template instance type.
TargetType
A target native data type.

Parameters

n [out]
A basic_node object to which the converted value is assigned.
val [in]
A native data object used for conversion.

Return Value

A basic_node object converted from a value of a compatible type.

Examples

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

namespace ns {

struct book {
    std::string title;
    std::string author;
    int year;
};

void to_node(fkyaml::node& n, const book& b) {
    n = {{"title", b.title}, {"author", b.author}, {"year", b.year}};
}

} // namespace ns

int main() {
    ns::book b = {"Noman's Journey", "John Doe", 2023};

    fkyaml::node n = b;

    std::cout << n << std::endl;

    return 0;
}

output:

author: John Doe
title: Noman's Journey
year: 2023

See Also