在这种情况下,我需要一种将可变数量的参数传递给函数的方法:

template<typename ...T>
struct Lunch
{
    Lunch(T...){}
};

template<typename T>
T CheckLuaValue(lua_State* luaState,int index)
{
    //Do Stuff
    return value;
}

template <class MemberType, typename ReturnType, typename... Params>
struct MemberFunctionWrapper <ReturnType (MemberType::*) (Params...)>
{
    static int CFunctionWrapper (lua_State* luaState)
    {
        ReturnType (MemberType::*)(Params...) functionPointer = GetFunctionPointer();
        MemberType* member = GetMemberPointer();

        int index = 1;

        //Get a value for each type in Params
        Lunch<Params...>
        {
           (CheckLuaValue<Params>(luaState,index), index++, void(), 0)...
        };

CheckLuaValue返回Params中每种类型的值。我的问题是我现在需要一种使用所有这些值来调用函数的方法
        member->*functionPointer(returnedLuaValues);

    }
};

我将如何去做呢?

最佳答案

因此,我偷了Luc Danton的一些关于sFuller和Pubby的序列建议(关于序列),并且生成了这个“停止与operator,混为一谈”的版本:

#include <iostream>
struct lua_State {};
template<typename T>
T CheckLuaValue( int n, lua_State* l)
{
    std::cout << "arg[" << n << "] gotten\n";
    return T(n);
}

template<int ...>
struct seq { };

// generates a seq< First, ..., Last-1 > as "type"
template<int First, int Last>
struct gen_seq
{
  template<int N, int... S>
  struct helper : helper<N-1, N-1, S...> {};
  template<int... S>
  struct helper<First, S...> {
    typedef seq<S...> type;
  };
  typedef typename helper<Last>::type type;
};

template< typename X >
struct MemberFunctionWrapper;

template< typename F >
struct MemberFunctionHelper
{
    typedef F MethodPtr;
};
template<class InstanceType, typename ReturnType, typename... Params>
struct MemberFunctionWrapper< ReturnType(InstanceType::*)(Params...) >
{
  typedef MemberFunctionHelper<ReturnType(InstanceType::*)(Params...)> Helper;
  typedef typename Helper::MethodPtr MethodPtr;
  static MethodPtr& GetFunctionPointer() {static MethodPtr pFunc; return pFunc;}
  static InstanceType*& GetMemberPointer() {static InstanceType* pThis;return pThis;}
  template<int n, typename Param>
  static auto GetLuaValue( lua_State* luaState )->decltype(CheckLuaValue<Param>(n,luaState))
  {
    return CheckLuaValue<Param>(n,luaState);
  }

  template< typename sequence >
  struct call;

  template< int... I >
  struct call<seq<I...>>
  {
    ReturnType operator()( lua_State* luaState, InstanceType* instance, MethodPtr method ) const
    {
      return (instance->*method)( GetLuaValue<I,Params>( luaState )... );
    }
  };
  static int CFunctionWrapper( lua_State* luaState)
  {
    MethodPtr func = GetFunctionPointer();
    InstanceType* instance = GetMemberPointer();
    ReturnType retval = call< typename gen_seq< 1, sizeof...(Params)+1 >::type >()( luaState, instance, func );
    return 0;
  }
};

struct test{ int foo(int x, double d){std::cout << "x:" << x << " d:" << d << "\n";}};
int main(){
    typedef MemberFunctionWrapper< int(test::*)(int, double) > wrapper;
    test bar;
    wrapper::GetFunctionPointer() = &test::foo;
    wrapper::GetMemberPointer() = &bar;
    wrapper::CFunctionWrapper(0);
}

现在,请注意对CheckLuaValue的调用可能是乱序的(即,它可以在参数1之前从Lua要求参数2),但是正确的参数将传递给正确的参数。

这是一个测试运行:http://ideone.com/XVmQQ6

关于c++ - 我将如何 'generate variadic parameters'?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13447063/

10-11 15:24