相关疑难解决方法(0)

为每个可变参数模板参数和数组调用函数

所以我有一些类型X:

typedef ... X;
Run Code Online (Sandbox Code Playgroud)

和模板功能f:

class <typename T>
void f(X& x_out, const T& arg_in);
Run Code Online (Sandbox Code Playgroud)

然后是一个功能g:

void g(const X* x_array, size_t x_array_size);
Run Code Online (Sandbox Code Playgroud)

我需要编写一个variadic模板函数h来执行此操作:

template<typename... Args>
void h(Args... args)
{
    constexpr size_t nargs = sizeof...(args); // get number of args
    X x_array[nargs]; // create X array of that size

    for (int i = 0; i < nargs; i++) // foreach arg
        f(x_array[i], args[i]); // call f (doesn't work)

    g(x_array, nargs); // call g with …
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-templates c++11

25
推荐指数
3
解决办法
3万
查看次数

标签 统计

c++ ×1

c++11 ×1

templates ×1

variadic-templates ×1