小编wja*_*jan的帖子

具有多个相同类型参数的模板函数

我正在尝试创建一个函数,该函数可以采用相同类型的多个参数,并作为模板传入。参数的数量在编译时是已知的:

struct Foo
{
    int a, b, c;
};

template <uint32_t argsCount, typename T>
void fun(T ...args) // max number of args == argsCount
{
    // ...
    // std::array<T, argsCount>{ args... };
}

int main()
{
    fun<3, Foo>( { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } );

    // Dont want to do:
    // fun( Foo{}, Foo{}, Foo{} );
    // nor:
    // fun<Foo, Foo, Foo>( ... );

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我必须考虑这些限制:

  • 没有堆内存分配
  • 没有 va_args …

c++ templates variadic c++14

9
推荐指数
3
解决办法
2904
查看次数

标签 统计

c++ ×1

c++14 ×1

templates ×1

variadic ×1