| 4 | #include <iostream> |
| 5 | |
| 6 | int 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 | } |
nothing calls this directly
no test coverage detected