更新时间:2022-10-15 08:21:44
使用Java varags,所有参数都是相同的类型。它与传递数组和它的大小基本相同。
所以你可以改用这样的东西:void foo( int * arr, size_t size);
void foo(std :: vector< int>& arr);
限制模板到特定类型只是模板的意图。但是,有可能之前已经被问: C ++ - 限制可变参数模板参数 - 堆栈溢出 [ ^ ]。
I'm struggling to find a C++ (14) equivalent for the varargs we know from Java.
What I have tried:
I kinda (but not fully) understand variadic templates and functions, so what I think I'd like to do is something like this:
template <typename... A>
void foo(A... a) { /*...*/ }
Now, while this works for a variable amount of parameters, I'd also like to be able to specify of which type all the types in A should be. For example in Java one could do:
void oof(int... i) { /*...*/ }
Is there a clean way to achieve this in C++ 14?
With Java varags, all parameters are of the same type. It is basically the same as passing an array and it's size.
So you might use something like this instead:void foo(int *arr, size_t size); void foo(std::vector<int>& arr);
Limiting templates to specific types only is just not the intention of templates. But it is possible and has been asked before: c++ - Restrict variadic template arguments - Stack Overflow[^].