# sol2 3.5.0 documentation

## variadic_args

### Search the Documentation

# variadic_args

transparent argument to deal with multiple parameters to a function

    struct variadic_args;

This class is meant to represent every single argument at its current index and beyond in a function list. It does not increment the argument count and is thus transparent. You can place it anywhere in the argument list, and it will represent all of the objects in a function call that come after it, whether they are listed explicitly or not.

`variadic_args` also has `begin()` and `end()` functions that return (almost) random-acess iterators. These return a proxy type that can be implicitly converted to a type you want, much like the table proxy type.

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4#include <iostream>
     5
     6int main() {
     7	std::cout << "=== variadic_args ===" << std::endl;
     8
     9	sol::state lua;
    10	lua.open_libraries(sol::lib::base);
    11
    12	// Function requires 2 arguments
    13	// rest can be variadic, but:
    14	// va will include everything after "a" argument,
    15	// which means "b" will be part of the varaidic_args list
    16	// too at position 0
    17	lua.set_function(
    18	     "v", [](int a, sol::variadic_args va, int /*b*/) {
    19		     int r = 0;
    20		     for (auto v : va) {
    21			     int value
    22			          = v; // get argument out (implicit
    23			               // conversion) can also do int v =
    24			               // v.as<int>(); can also do int v =
    25			               // va.get<int>(i); with index i
    26			     r += value;
    27		     }
    28		     // Only have to add a, b was included from
    29		     // variadic_args and beyond
    30		     return r + a;
    31	     });
    32
    33	lua.script("x = v(25, 25)");
    34	lua.script("x2 = v(25, 25, 100, 50, 250, 150)");
    35	lua.script("x3 = v(1, 2, 3, 4, 5, 6)");
    36	// will error: not enough arguments!
    37	// lua.script("x4 = v(1)");
    38
    39	lua.script("assert(x == 50)");
    40	lua.script("assert(x2 == 600)");
    41	lua.script("assert(x3 == 21)");
    42	lua.script("print(x)");  // 50
    43	lua.script("print(x2)"); // 600
    44	lua.script("print(x3)"); // 21
    45
    46	std::cout << std::endl;
    47
    48	return 0;
    49}

You can also “save” arguments and the like later, by stuffing them into a `std::vector<sol::object>` or something similar that serializes them into the registry. Below is an example of saving all of the arguments provided by `sol::variadic_args` in a lambda capture variable called `args`.

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4#include <iostream>
     5#include <functional>
     6
     7int main() {
     8
     9	std::cout << "=== variadic_args serialization/storage ==="
    10	          << std::endl;
    11
    12	sol::state lua;
    13	lua.open_libraries(sol::lib::base);
    14
    15	std::function<void()> function_storage;
    16
    17	auto store_routine = [&function_storage](sol::function f,
    18	                          sol::variadic_args va) {
    19		function_storage =
    20		     [f,
    21		          args = std::vector<sol::object>(va.begin(),
    22		               va.end())]() { f(sol::as_args(args)); };
    23	};
    24
    25	lua.set_function("store_routine", store_routine);
    26
    27	lua.script(R"(
    28function a(name)
    29	print(name)
    30end
    31store_routine(a, "some name")
    32)");
    33	function_storage();
    34
    35	lua.script(R"(
    36function b(number, text)
    37	print(number, "of", text)
    38end
    39store_routine(b, 20, "these apples")
    40)");
    41	function_storage();
    42
    43	std::cout << std::endl;
    44
    45	return 0;
    46}

Finally, note that you can use `sol::variadic_args` constructor to “offset”/”shift over” the arguments being viewed:

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4#include <iostream>
     5
     6int main() {
     7
     8	std::cout << "=== variadic_args shifting constructor ==="
     9	          << std::endl;
    10
    11	sol::state lua;
    12	lua.open_libraries(sol::lib::base);
    13
    14	lua.set_function("f", [](sol::variadic_args va) {
    15		int r = 0;
    16		sol::variadic_args shifted_va(va.lua_state(), 3);
    17		for (auto v : shifted_va) {
    18			int value = v;
    19			r += value;
    20		}
    21		return r;
    22	});
    23
    24	lua.script("x = f(1, 2, 3, 4)");
    25	lua.script("x2 = f(8, 200, 3, 4)");
    26	lua.script("x3 = f(1, 2, 3, 4, 5, 6)");
    27
    28	lua.script("print(x)");  // 7
    29	lua.script("print(x2)"); // 7
    30	lua.script("print(x3)"); // 18
    31
    32	std::cout << std::endl;
    33
    34	return 0;
    35}