访问单个类型的可变模板 [英] Accessing individual types of variadic templates

查看:146
本文介绍了访问单个类型的可变模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++ 11中创建了一个lua绑定。我想在一个可变参数模板中处理每个类型。



我想我可以这样做,除了使用 Params ... 表示其中的所有类型,而不是其中的下一个单独类型,如可变参数函数参数。

  template< class T,typename ReturnType,typename ... Params> 
struct MemberFunctionWrapper< ReturnType(T :: *)(Params ...)>
{

static int CFunctionWrapper(lua_State * luaState)
{
for(int i = 0; i {
//我想获得下一个类型,而不是所有的类型
CheckLuaValue< Params ...>();
//做其他事情
}
}
};

我该如何做?

 

您可以通过扩展函数调用后扩展到

//把它放在你的命名空间中
struct Lunch {template< typename ... T>午餐(T ...){}};

//而不是for循环
午餐{(CheckLuaValue< Params>(),void(),0)...}

您可以使用lambda做其他事情。你甚至可以让你的 i 递增

  static int CFunctionWrapper(lua_State * luaState)
{
int i = 0;
Lunch {
(CheckLuaValue< Params>(),
[&] {std :: cout<<param<< i< std :: endl;}(),
++ i)...
};
}

请注意,标准支持将所有内容放入lambda。编译器支持直到最近(上次我检查)不是很好,虽然

  static int CFunctionWrapper(lua_State * luaState)
{
int i = 0;
Lunch {([&] {
CheckLuaValue< Params>();
std :: cout<<<This is param<< i< std :: endl;
}(),++ i)...
};
}


I am creating a lua binding in C++11. I want to process each type in a variadic template.

I was thinking I could do something like this, except using Params... represents all of the types inside of it, and not a the next single type inside of it like variadic function parameters do.

template <class T, typename ReturnType, typename... Params>
struct MemberFunctionWrapper <ReturnType (T::*) (Params...)>
{

    static int CFunctionWrapper (lua_State* luaState)
    {
        for(int i = 0; i < sizeof...(Params); i++)
        {
             //I want to get the next type, not all of the types
             CheckLuaValue<Params...>();
             //Do other stuff
        }
    }
};

How would I go about doing this?

解决方案

You can do this by simply expanding after the function call, into something that can be expanded to.

// put this in your namespace
struct Lunch { template<typename ...T> Lunch(T...) {} }; 

// and this instead of the for loop
Lunch{ (CheckLuaValue<Params>(), void(), 0)... };

You can do something else with a lambda. You can even have your i incremented

static int CFunctionWrapper (lua_State* luaState)
{
    int i = 0;
    Lunch{ 
      (CheckLuaValue<Params>(), 
       [&]{ std::cout << "That was param " << i << std::endl; }(),
       ++i)... 
    };
}

Note that the Standard supports putting everything into the lambda. Compiler support until recently (last time I checked) wasn't very good though

static int CFunctionWrapper (lua_State* luaState)
{
    int i = 0;
    Lunch{([&]{ 
       CheckLuaValue<Params>();
       std::cout << "That was param " << i << std::endl;
    }(), ++i)... 
    };
}

这篇关于访问单个类型的可变模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆