Skip to content

Defined in header <fkYAML/node.hpp>

fkyaml::basic_node::(constructor)

basic_node() = default; // (1)

explicit basic_node(const node_type type); // (2)

explicit basic_node(const node_t type); // (3) deprecated

basic_node(const basic_node& rhs); // (4)

basic_node(basic_node&& rhs) noexcept; // (5)

template <typename CompatibleType, typename U = detail::remove_cvref_t<CompatibleType>>
basic_node(CompatibleType&& val) noexcept(
    noexcept(ConverterType<U>::to_node(std::declval<basic_node&>(), std::declval<CompatibleType>()))); // (6)

basic_node(initializer_list_t init); // (7)

Constructs a new basic_node from a variety of data sources.
Available overloads are:

  1. Constructs a basic_node with a null value.
    The resulting basic_node has the node_type::NULL_OBJECT type.
  2. Constructs a basic_node with the given type.
    The resulting basic_node has a default value for the given type.

    type default value
    node_type::SEQUENCE (empty sequence)
    node_type::MAPPING (empty mapping)
    node_type::NULL_VALUE nullptr
    node_type::BOOLEAN false
    node_type::INTEGER 0
    node_type::FLOAT 0.0
    node_type::STRING (empty string)
  3. Constructs a basic_node with the given type.
    The resulting basic_node has a default value for the given type.
    Default values are the same as those in the above table.

    Deprecation

    The overload(2) basic_node(const node_type); replaces the overload(3) basic_node(const node_t); which has been deprecated in version 0.3.12. It will be removed in a future version. Please replace calls like

    fkyaml::node n(fkyaml::node::node_t::MAPPING);
    

    with

    fkyaml::node n(fkyaml::node_type::MAPPING);
    
  4. Copy constructs a basic_node with an existing basic_node.

  5. Move constructs a basic_node with an existing basic_node.
    The existing basic_node will be the same as a default-constructed basic_node at the end of this constructor.
  6. Constructs a basic_node with a value of a compatible type (see below).
    The resulting basic_node has the value of val and the type which is associated with CompatibleType.
    Template parameter CompatibleType includes, but not limited to, the following types:

    • sequences:
      • sequence_type
      • container types which fulfills the following requirements, e.g., std::vector or std::set.
        • Both begin() and end() are callable on a CompatibleType object.
        • CompatibleType doesn't have both key_type and mapped_type member types.
        • A string_type object is not constructible from a CompatibleType object.
      • std::pair and std::tuple
    • mappings
      • mapping_type
      • container types which fullfills the following requirements, e.g., std::map or std::unordered_multimap.
        • Both begin() and end() are callable on a CompatibleType object.
        • CompatibleType has both key_type and mapped_type member types.
    • null
    • booleans
    • integers
      • integer_type
      • all integral types except bool such as int, uint32_t or size_t.
    • floating point values
    • strings

    You can add types to meet your needs by implementing custom to_node() functions. See node_value_converter for details. 7. Constructs a basic_node with a initializer_list_t object.
    The resulting basic_node has the value of a container (sequence or mapping) which has the contents of init.
    Basically, init is considered to be a sequence.
    If init contains a sequence where each element contains 2 basic_node objects, however, such a sequence is interpreted as a mapping.

    initializer lists which have a single element

    fkyaml::node n {true}; will create a sequence of 1 boolean value, not a boolean value alone. This is because overload resolution in C++ prioritizes a constructor with a single initializer list argument is prioritized to the others. In such cases, change the code to fkyaml::node n(true); instead.

Template Parameters

CompatibleType
A type such that ConverterType<U> has a to_node function.
U
A type referred to by ValueType with its topmost cv-qualifiers removed.

Parameters

type [in]
the type of the value to create
rhs [in]
the basic_node object to copy/move the value from
val [in]
the value of a compatible type.
init [in]
initializer list of basic_node objects

Examples

Overload(1): create a default value
#include <iostream>
#include <fkYAML/node.hpp>

int main() {
    fkyaml::node n;
    std::cout << n << std::endl;
    return 0;
}

output:

null

Overload(2): create an empty value with a given type
#include <iostream>
#include <fkYAML/node.hpp>

int main() {
    fkyaml::node n(fkyaml::node_type::INTEGER);
    std::cout << n << std::endl;
    return 0;
}

output:

0

Overload(3): create an empty value with a given type (deprecated)
#include <iostream>
#include <fkYAML/node.hpp>

int main() {
    fkyaml::node n(fkyaml::node::node_t::INTEGER);
    std::cout << n << std::endl;
    return 0;
}

output:

0

Overload(4): copy construct a basic_node object
#include <iostream>
#include <fkYAML/node.hpp>

int main() {
    fkyaml::node n(fkyaml::node::node_t::BOOLEAN);
    fkyaml::node n2(n);
    std::cout << n2 << std::endl;
    return 0;
}

output:

false

Overload(5): move construct a basic_node object
#include <iostream>
#include <fkYAML/node.hpp>

int main() {
    fkyaml::node n(fkyaml::node::node_t::BOOLEAN);
    fkyaml::node n2(std::move(n));
    std::cout << n2 << std::endl;
    return 0;
}

output:

false

Overload(6): create a basic_node object with compatible types
#include <iostream>
#include <list>
#include <tuple>
#include <unordered_map>
#include <fkYAML/node.hpp>

int main() {
    // create nodes from objects of various types.
    fkyaml::node sequence0 = std::list<bool> {true, false, false};
    fkyaml::node sequence1 = std::make_tuple<std::string, int, bool>("foo", 123, true);
    std::unordered_map<int, float> map_val = {{123, 3.14f}, {-456, 1.41f}};
    fkyaml::node mapping = std::move(map_val);
    fkyaml::node null = nullptr;
    fkyaml::node boolean = false;
    fkyaml::node integer = 12345;
    fkyaml::node floating_point = 3.141592;
    const char str_chars[] = "test";
    fkyaml::node string = str_chars;

    // print node values
    std::cout << sequence0 << std::endl;
    std::cout << sequence1 << std::endl;
    std::cout << mapping << std::endl;
    std::cout << null << std::endl;
    std::cout << boolean << std::endl;
    std::cout << integer << std::endl;
    std::cout << floating_point << std::endl;
    std::cout << string << std::endl;
    return 0;
}

output:

- true
- false
- false

- foo
- 123
- true

-456: 1.41
123: 3.14

null
false
12345
3.14159
test

Overload(7): create a basic_node object from an initializer list
#include <iostream>
#include <fkYAML/node.hpp>

int main() {
    // create a sequence.
    fkyaml::node n = {true, false};
    std::cout << n << std::endl;

    // create a mapping.
    fkyaml::node n2 = {{"foo", 1024}};
    std::cout << n2 << std::endl;
    return 0;
}

output:

- true
- false

foo: 1024


See Also