I have a template function
template <class T>
void foo() {
// Within this function I need to create a new T
// with some parameters. Now the problem is I don't
// know the number of parameters needed for T (could be
// 2 or 3 or 4)
auto p = new T(...);
}
How do I solve this? Somehow I remember saw functions with input like (..., ...)?
You could use variadic templates:
template <class T, class... Args>
void foo(Args&&... args){
//unpack the args
T(std::forward<Args>(args)...);
sizeof...(Args); //returns number of args in your argument pack.
}
This question here has more detail on how to unpack arguments from a variadic template. This question here may also provide more information
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With