passing unlimited arguments

hi c++ geeks
i was wondering if i can pass infinite arguments to my function so i don't want
it to be limited like this for example

1
2
3
4
5
 void Example (int a , int b ) 
{
cout << a << endl ;
cout << b << endl;
}

i don't want to be limited like that .. i just want to pass as many arguments as i want when i call the function and i want the function to print all of my arguments. i know there is a thing called argc and argv but they - as i know - only work with main function and i want them to work also in any function that i declared if it is possible
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

template< typename T > void print( const T& v ) { std::cout << v << ' ' ;  }

// http://www.stroustrup.com/C++11FAQ.html#variadic-templates
template< typename FIRST, typename... REST > void print( const FIRST& first, const REST&... rest )
{
    print(first) ;
    print(rest...) ;
}

int main()
{
    print( 123, 78.46, "hello world!", std::addressof(std::cout), '\n' ) ;
}

http://coliru.stacked-crooked.com/a/1de964497bb43045
you know they both are working

but somehow i get the first one

but the seccond one i can't understand it cuz i don't know what template is ?
Then perhaps you should google c++ template? Youtube works too.
ok i'll read about , but mainly i think that the problem has been solved
You don't indicate if you want an unlimited number of arguments of different types.

If all the arguments are of the same type, passing a simple array (or vector) should suffice for what you want.


actually i want them the same type
Topic archived. No new replies allowed.