0
votes

So for this function:

template<typename T>
T popObject(ScriptObject obj) { //Preforms a cast from void* on each to its type.
    assert(obj.getType() == std::type_index(typeid(T))); //Ensure type Safety.
    return obj.get<T>();
}

template <typename C, typename...Args>
    void bind(const char* name, C* context, void(C::* function)(Args...)) {
        std::vector<std::type_index> arguments = { std::type_index(typeid(Args))... };
        auto functor = [](void* func,void* contex, std::vector<ScriptObject> stack) {
            std::size_t idx = 0;
            //Call class context -> function (Args...)
        };
        _functions[name] = new BoundFunction(functor, void_cast(function), context, arguments);
    }

I'm unsure of the syntax for calling the member function.

Iv tried:

union MF{
    void(C::* pf)(Args...);
    void* p;
} mf;
mf.p = func;
contex->*(mf.pf)(popObject<Args>(stack[idx++])...);

Which does not compile, Stating: "Does not evaluate to a function taking 1 Arguments."

Tried std::invoke, But I Don't know how to Specify the Member function Signature for arg1.

So If i have a reference to the member function, And a reference to the instance of the class, As well as the template information to Derive type information. How do I call the Classes member function?

1
Shot in the dark, still looking it over, have you tried popObject<Args...>? Or changing static_cast<C*>(contex).* to static_cast<C*>(contex)->? Also why are you casting pf to a type it already is?The Floating Brain
(context->*(mf.pf))(...). First, it's ->* not .*, since you have a pointer on the left. Second, note the parentheses around context->*(mf.pf) part.Igor Tandetnik
@TheFloatingBrain Thats a no go, Args... Would evaluate to template with each of the arguments, I still Want popObject to run on each Individual Argument. And -> only works if the member function name is known.Steven Venham
@StevenVenham Sorry misread, but you should take a look at Igor's comment. Also if your function pointer pf takes in Args... does popObject return the type it gets from unfolding the pack?The Floating Brain
Ok So changed to static_cast<C*>(ctx)->*(mf.pf)(popObject<Args>(stack[idx++])...); Good call on the extra static cast @TheFloatingBrain And Iv changed it to ->* IgorSteven Venham

1 Answers

1
votes

Thanks @Igor Tandentnik

Final Solution was:

(static_cast<C*>(ctx)->*(mf.pf))(popObject<Args>(stack[idx++])...);

As Igor pointed out, it needed to be ->* instead of .*, and added parenthesis, as -> has a lower priority then the Function call operator.

Also, thanks @TheFloatingBrain for pointing out the extra static_cast