+ 3
How do I declare a variadic function in C++
Sorry for this simple question I'm still somewhat new to C++
6 ответов
+ 4
Variadic templates (C++11)
https://en.cppreference.com/w/cpp/language/parameter_pack
https://en.wikipedia.org/wiki/Variadic_template
C style variadic functions are to be avoided in C++.
Example:
#include <iostream>
// Base case, just like in recursion
template<typename T>
T sum( T value )
{
return value;
}
// Variadic function
template<typename T, typename... Args>
T sum( T value, Args... args )
{
return value + sum( std::forward<Args>(args)... );
// return value + sum( args... ); should be ok as well here.
}
int main()
{
std::cout << sum( 1, 3, 4, 3 );
}
+ 3
here is an example without using recursion:
https://code.sololearn.com/cp7M3NWL9A9P/#cpp
+ 2
How about initializer_list<T>?
+ 1
MOIN KHAN
That's not a variadic function, m8.
0
Simply make a function and pass variable arguments to it using function overloading like
int example(int a, int b)
{.
.
.
return(i,j)}
0
May i can be wrong