# sol2 3.5.0 documentation

## nested

### Search the Documentation

# nested

    template <typename T>
    struct nested {
            T& value() &;
            const T& value() & const;
            T&& value() &&;
    };

`sol::nested<...>` is a template class similar to sol::as_table, but with the caveat that every container type within the `sol::nested` type will be retrieved as a table from lua. This is helpful when you need to receive C++-style vectors, lists, and maps nested within each other: all of them will be deserialized from lua using table properties rather than anything else.

Note that any caveats with Lua tables apply the moment it is serialized, and the data cannot be gotten out back out in C++ as a C++ type. You can deserialize the Lua table into something explicitly using the `sol::as_table_t` marker for your get and conversion operations using sol. At that point, the returned type is deserialized from a table, meaning you cannot reference any kind of C++ data directly as you do with regular userdata/usertypes. All C++ type information is lost upon serialization into Lua.

The example provides a very in-depth look at both `sol::as_table<T>` and `sol::nested<T>`, and how the two are equivalent.

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4#include <vector>
     5#include <map>
     6#include <iostream>
     7
     8
     9// nested allows serialization of maps with vectors inside, and
    10// vice-versa all from a nested structure of Lua tables it has
    11// less control over which pieces are considered tables in Lua,
    12// and which ones are considered userdata, but it covers a good
    13// 90% of cases where someone wants to handle a nested table
    14void demo(
    15     sol::nested<std::map<std::string, std::vector<std::string>>>
    16          src) {
    17	std::cout << "demo, sol::nested<...>" << std::endl;
    18	const auto& listmap = src.value();
    19	SOL_ASSERT(listmap.size() == 2);
    20	for (const auto& kvp : listmap) {
    21		const std::vector<std::string>& strings = kvp.second;
    22		SOL_ASSERT(strings.size() == 3);
    23		std::cout << "\t" << kvp.first << " = ";
    24		for (const auto& s : strings) {
    25			std::cout << "'" << s << "'"
    26			          << " ";
    27		}
    28		std::cout << std::endl;
    29	}
    30	std::cout << std::endl;
    31		std::cout << "\t" << kvp.first << " = ";
    32		for (const auto& s : strings) {
    33			std::cout << "'" << s << "'"
    34			          << " ";
    35		}
    36		std::cout << std::endl;
    37	std::cout << std::endl;
    38}
    39
    40int main(int, char**) {
    41	std::cout << "=== containers retrieved from lua tables ==="
    42	          << std::endl;
    43	sol::state lua;
    44	// bind the function
    45	lua.set_function("f", &demo);
    46	lua.set_function("g", &demo_explicit);
    47	// Call it with a table that has string sequences set to
    48	// distinct keys
    49	lua.script(R"(
    50t = {
    51	key1 = {'hello', 'there', 'world'},
    52	key2 = {'bark', 'borf', 'woof'}
    53}
    54f(t)
    55g(t)
    56	)");
    57
    58	std::cout << std::endl;
    59
    60	return 0;
    61}