你的语法是错误的。您的功能等同于:
bool foo(int bar...)
{
std::vector<int> barList(bar...);
// ...
}
请注意,根本没有可变参数模板,也没有什么可解压的——相反,您创建了一个 C 风格的可变参数函数。
更改功能的一种最简单方法是:
template<typename... Args>
bool foo(Args... bar)
{
std::vector<std::tuple<std::string,float>> barList({bar...});
// ...
}
This is not ideal, as it makes your template function quite greedy - it will gladly consume any arguments, not just tuples of strings and floats.
We can spice it up by using C++20 concepts
:
template<class T>
concept Tuple = std::is_same_v<T, std::tuple<std::string, float>>;
template<Tuple... T>
bool foo(T... bar)
{
std::vector<std::tuple<std::string, float>> barList({bar...});
// ...
return true;
}
This allows usage like that:
foo(std::tuple<std::string, float>{"ddd", 20}, std::tuple<std::string, float>{"ddd", 20});
But not like that:
foo(10, 20, nullptr);