passing unlimited arguments

Dec 28, 2015 at 6:17pm
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
Dec 28, 2015 at 6:22pm
Dec 28, 2015 at 6:31pm
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
Dec 28, 2015 at 6:56pm
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 ?
Dec 28, 2015 at 7:06pm
Then perhaps you should google c++ template? Youtube works too.
Dec 28, 2015 at 7:19pm
ok i'll read about , but mainly i think that the problem has been solved
Dec 28, 2015 at 7:53pm
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.


Dec 30, 2015 at 3:46pm
actually i want them the same type
Topic archived. No new replies allowed.