Код: Выделить всё
template
type_t stack_value_as(lua_State *const L, int index)
{
// template magic happens here…
return {};
}
template
type_t table_value_at(lua_State *const L, int table_stack_position, int index)
{
lua_rawgeti(L, table_stack_position, index);
auto result = stack_value_as(L, -1);
lua_pop(L, 1);
return result;
}
template
auto read_tuple(lua_State *L, int index)
{
using tuple_t = std::tuple;
if (lua_istable(L, index))
{
const auto size_lua = lua_rawlen(L, index);
constexpr auto size_cpp = sizeof...(type_pack);
if (size_lua == size_cpp)
{
auto helper = [&](const std::integer_sequence &)
{
return tuple_t{ table_value_at(L, index, index_pack - index) ... };
};
return helper(std::make_integer_sequence{});
}
}
return tuple_t{};
}
< /code>
Когда я тестирую этот код с помощью скрипта Lua ниже, он не удастся: < /p>
test = {e = 42, n = 3.1415, id = "test, test, 1, 2, 3..."};
-- call 1: ok
tup({1, 2.3456, "789"})
-- call 2 & 3: lua_istable is true, lua_rawlen is 0
tup(test)
tup({e = 42, n = 3.1415, id = "test, test, 1, 2, 3..."})
Подробнее здесь: https://stackoverflow.com/questions/768 ... s-stdtuple