2

I want to copy a function from one Lua_State to another using luabridge.

luabridge provides a Function called addFunction(const char * name,FP fp) and an Function called getGlobal(lua_State* L,const char*) which returns a Object of Type LuaRef which has overloaded operators. I am using a multimap to store the names of the functions I want to copy.

the function addFunction() does not support the usage of a pointer to a class, therefore i cannot pass getGlobal().operator() directly

    //get all functions that match the Criteria
    std::pair<acc_map_iter, acc_map_iter> range = sec_map->equal_range(acl);

    //Add them to the first State
    std::for_each(range.first, range.second, [&](acc_map_iter iter){
        script->compilerContext->addFunction(iter->second.c_str(), [&](/*args...?*/)
        {
            return luabridge::getGlobal(sec_state, iter->second.c_str()).operator(/*args...?*/);
        });
    });

Can I somehow make the lambda accept multiple arguments from addFunction(). Is there a trick or is it simply impossible?

Raphl10
  • 56
  • 2
  • 6

1 Answers1

0

I'm not sure I got the problem, but it seems that you need a generic lambda.
Yes, you can have them. Here is a minimal, working example:

#include<utility>

struct S {
    void g(int) { }

    template<typename... Args>
    void f(Args&&... args) {
        auto fn = [this](auto&&... args){
            g(std::forward<decltype(args)>(args)...);
        };
        fn(std::forward<Args>(args)...);
    }
};

int main() {
    S s;
    s.f(42);
}

It's a matter of an ugly statement like the one involving the decltype to forward the arguments.

skypjack
  • 49,335
  • 19
  • 95
  • 187
  • i don't think this would work in my case since the types of the argument could not be deduced using f() because f() woud not actually call the function to do something – Raphl10 May 22 '16 at 12:52