Defined in header <fkYAML/node_value_converter.hpp>
fkyaml::basic_node::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 native data to a basic_node
object.
This function is usually called by the constructors of the basic_node
class.
Tips
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 for more information.
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.
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: