Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
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:
Java
void oof(int... i) { /*...*/ }

Is there a clean way to achieve this in C++ 14?
Posted
Updated 4-Jun-18 0:00am

1 solution

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:
C++
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[^].
 
Share this answer
 
Comments
CPallini 4-Jun-18 6:22am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900