小编Dmi*_*yev的帖子

使用可变模板解包参数列表时获取参数索引

我需要在解包和转换参数列表时获取参数的索引。有没有以下问题的解决方案:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

void test(int a, std::string b, bool c)
{
    cout << a << "," << b << "," << c << endl ;
}

template <typename... ARG>
static void call_test(const vector<void*> &params)
{
    test(*static_cast<ARG*>(params[ indexOf(ARG) ])...);
}

int main(int argc, char **argv)
{
    int    a = 1;
    string b = "string";
    bool c   = false;
    vector<void*> v(3);
    v[0] = &a;
    v[1] = &b;
    v[2] = &c;

    call_test<int,string,bool>(v);
}
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-templates c++11

3
推荐指数
1
解决办法
2859
查看次数

标签 统计

c++ ×1

c++11 ×1

templates ×1

variadic-templates ×1