-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariadicParameterExmaple
41 lines (33 loc) · 1.05 KB
/
VariadicParameterExmaple
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <initializer_list>
//This example copied from Stackoverflow.com .I can't understand how to use ... yet
//What I understand now is you can't declare ... in a normal function defination , but I don't know how to use it yet .
template <typename T>
void func(T t)
{
std::cout << t << std::endl ;
}
template<typename T, typename... Args>
void func(T t, Args... args) // recursive variadic function
{
std::cout << t <<std::endl ;
func(args...) ;
}
template <class T>
void func2( std::initializer_list<T> list ) //the function demonstrates how to use standard initializer list
{ //initializer list is used for variable number of variables of same type
for( auto elem : list )
{
std::cout << elem << std::endl ;
}
}
int main()
{
std::string
str1( "Hello" ),
str2( "world" );
func(1,2.5,'a',str1); //passing ,int ,float , char , string into a variadic function
func2( {10, 20, 30, 40 }) ; //demo 1
func2( {str1, str2 } ) ; //demo 2
}