小编K.K*_*der的帖子

如何将向量(或类似向量)传递到可变参数模板中

假设我有以下代码:

template <typename... Args>
void DoSomething(const Args&... args)
{
    for (const auto& arg : {args...})
    {
        // Does something
    }
}
Run Code Online (Sandbox Code Playgroud)

现在假设我从另一个函数调用它,并且想要传递一个std::vector(或者以某种方式修改向量,使其可以与此一起使用)

void DoSomethingElse()
{
    // This is how I'd use the function normally
    DoSomething(50, 60, 25);

    // But this is something I'd like to be able to do as well
    std::vector<int> vec{50, 60, 25};
    DoSomething(??); // <- Ideally I'd pass in "vec" somehow
}
Run Code Online (Sandbox Code Playgroud)

有办法做到这一点吗?我也考虑过使用std::initializer_list而不是可变参数模板,但问题仍然是我无法传递现有数据。

谢谢。

c++ templates variadic-templates c++11

5
推荐指数
1
解决办法
2442
查看次数

标签 统计

c++ ×1

c++11 ×1

templates ×1

variadic-templates ×1