I have a variadic function accepting any number of mixed parameters: the following code works as expected:
template <typename ... Args> void call_snippet(lua_State *L, const std::string& name, Args... args) { lua_rawgeti(L, LUA_REGISTRYINDEX, snippets[name]); int nargs = 0; for (auto &&x : {args...}) { lua_pushinteger(L, x); nargs++; } lua_pcall(L, nargs, LUA_MULTRET, 0); } but falls short from needs because it assumes all parameters are int (or convertible to int). I would need something along the lines:
template <typename ... Args> void call_snippet(lua_State *L, const std::string& name, Args... args) { lua_rawgeti(L, LUA_REGISTRYINDEX, snippets[name]); int nargs = 0; for (auto &&x : {args...}) { switch (typeof(x)) { case int: lua_pushinteger(L, x); nargs++; break; case float: lua_pushnumber(L, x); nargs++; break; case std:string: lua_pushcstring(L, x.c_str()); nargs++; break; case char*: lua_pushcstring(L, x); nargs++; break; default: //raise error ; } lua_pcall(L, nargs, LUA_MULTRET, 0); } How should I actually implement the above pseudocode?
lua_pushinteger(L, x);withpush_something_somewhere(L, x), and then defining overloaded functionspush_something_somewhere(), one that takes anintparameter, another that takes afloat, a third one that takes aconst std::string &, etc...? You never do this kind of thing yourself, manually. That's yucky. You simply let your compiler do all the yucky work for you.