Skip to content

variadic templates #119

@parnet

Description

@parnet

Classes like LuaFunction uses variadicts function arguments, but not template arguments. this leads to the restriction for the function that the variadicts can only process the same type.

however with the newer lua version it is possible to distinguish more types, and it might also be necessary to process other types within functions on c++ side which are set by lua.

this already affects the mentioned class since the lua-callback function is called with int instead of number a quick fix for this was to implement a specialized class in upgrade/modernize with name LuaCallbackFunction which is basically a copy of the original class that tries to call with int.

adapt lua traits with c++17 fold expression:

// Variadic template to push arbitrary arguments
template<typename... Args>
void push_args(lua_State* L, Args&&... args) {
    // Fold expression in C++17
    (lua_traits<std::decay_t<Args>>::push(L, std::forward<Args>(args)), ...);
} ```


```cpp

template<typename... Args>
void example(lua_State* L, Args&&... args) {
    // Push all arguments to Lua stack
    push_args(L, std::forward<Args>(args)...);
    
    // ... lua_call(L, sizeof...(args), 0);
}

alternative variadic class templates:

template<typename... Args>
class ExampleClass {
ExampleClass(Args... args) : data(args...) {}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions