【发布时间】:2019-11-22 01:15:29
【问题描述】:
在现代 C++(即版本>=11)中,实现具有多个 int 参数的变量的函数的最佳方法是什么?
我只想要ints,不是一般的类型。
每个参数都是 int 类型,不允许使用其他类型。
void foo(/*WHAT'S HERE*/) {
// and how do I access the arguments here?
}
int main()
{
foo(34,1);
foo(9,2,66,1);
// etc
return 0;
}
【问题讨论】:
-
我在重复列表中添加了另一个问题,其中包括将参数限制为 int
-
为什么不传入
vector<int>? -
为什么这不起作用:ideone.com/TvLalR
-
@M.M 很多,很多,...,非常感谢。以下在 C++17 中工作,非常漂亮。
template <typename... U> typename std::enable_if<(std::is_same<U, int>::value && ...), void>:: type foo(U... ints) { const int size = sizeof...(ints); int intarray[size] = {ints...}; } -
@RFS 美女在旁观者的眼中 :)
标签: c++ c++11 c++14 c++17 variadic-templates