functions with variable number of arguments

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: functions with variable number of arguments

 
0
  #11
Nov 17th, 2007
all my attempts at generating the functions using boost.preprocessor have resulted in code that was difficult to read and difficult to debug. (i really do not know how to debug preprocessor code other than by looking at the generated cpp output. and none of the versions i tried ended up being very readable).

but there is good news. c++ programmers will never have to use the unsafe and error-prone <cstdarg> ; c++0x has a solution. using variadic templates, we can write a typesafe function taking an arbitrary number of arguments of arbitrary types. http://en.wikipedia.org/wiki/C++0x#Variadic_templates

here is how you would write largest_of a variable number of values in c++0x.
  1. #include <iostream>
  2. #include <string>
  3. // compile with gcc 4.3 with the -std=c++0x switch
  4.  
  5. template< typename T > inline
  6. const T& largest_of( const T& a, const T& b )
  7. { return a>b ? a : b ; }
  8.  
  9. template< typename T, typename ...REST > inline
  10. const T& largest_of( const T& first, const REST&... rest... )
  11. { return largest_of( first, largest_of( rest... ) ) ; }
  12.  
  13. int main()
  14. {
  15. std::cout << largest_of( 1, 9, 2, 8, 3, 7, 4, 6, 5 ) << '\n' ;
  16. std::cout << largest_of( 5.3, 8.9, -3.2, 17.8, 4.2, 7.0 )
  17. << '\n' ;
  18. const std::string s1("abc"), s2("2345"),
  19. s3("vufuk"), s4("ffyfyu") ;
  20. std::cout << largest_of( s1, s2, s3, s4 ) << '\n' ;
  21. }
  22. /**
  23. >g++43 -Wall -std=c++0x -pedantic -Werror largest_of.cc && ./a.out
  24. 9
  25. 17.8
  26. vufuk
  27. */
Last edited by vijayan121; Nov 17th, 2007 at 5:01 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,626
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: functions with variable number of arguments

 
0
  #12
Nov 17th, 2007
Originally Posted by vijayan121 View Post
>
but there is good news. c++ programmers will never have to use the unsafe and error-prone <cstdarg> ; c++0x has a solution. using variadic templates, we can write a typesafe function taking an arbitrary number of arguments of arbitrary types. http://en.wikipedia.org/wiki/C++0x#Variadic_templates

here is how you would write largest_of a variable number of values in c++0x.
  1. #include <iostream>
  2. #include <string>
  3. // compile with gcc 4.3 with the -std=c++0x switch
  4.  
  5. template< typename T > inline
  6. const T& largest_of( const T& a, const T& b )
  7. { return a>b ? a : b ; }
  8.  
  9. template< typename T, typename ...REST > inline
  10. const T& largest_of( const T& first, const REST&... rest... )
  11. { return largest_of( first, largest_of( rest... ) ) ; }
  12.  
  13. int main()
  14. {
  15. std::cout << largest_of( 1, 9, 2, 8, 3, 7, 4, 6, 5 ) << '\n' ;
  16. std::cout << largest_of( 5.3, 8.9, -3.2, 17.8, 4.2, 7.0 )
  17. << '\n' ;
  18. const std::string s1("abc"), s2("2345"),
  19. s3("vufuk"), s4("ffyfyu") ;
  20. std::cout << largest_of( s1, s2, s3, s4 ) << '\n' ;
  21. }
  22. /**
  23. >g++43 -Wall -std=c++0x -pedantic -Werror largest_of.cc && ./a.out
  24. 9
  25. 17.8
  26. vufuk
  27. */
Greate news for the graduating Class of 2020. There are no compilers that support it since the standards have not been approved yet so we still have to resort to stdarg.h to get variable argument functions.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: functions with variable number of arguments

 
0
  #13
Nov 17th, 2007
> There are no compilers that support it
the output that i posted is with the code compiled by gcc 4.3 today; they are not my visualization of how it would look like in 2020.

> we still have to resort to stdarg.h to get variable argument functions
you may have to. i do not. even without gcc 4.3.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,626
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: functions with variable number of arguments

 
0
  #14
Nov 17th, 2007
Originally Posted by vijayan121 View Post
> There are no compilers that support it
the output that i posted is with the code compiled by gcc 4.3 today; they are not my visualization of how it would look like in 2020.
You are fortunate -- I tried it with VC++ 2005 Express and Dev-D++ Version 4.9.9.2 and they both puked out lots of errors.

Originally Posted by vijayan121 View Post
> we still have to resort to stdarg.h to get variable argument functions you may have to. i do not. even without gcc 4.3.
Apparently I'm not alone! I suspect you think everyone should abandon their compilers and get gcc 4.3 (note: I'm not trying to insult you here, just poking a little fun)
Last edited by Ancient Dragon; Nov 17th, 2007 at 9:28 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: functions with variable number of arguments

 
1
  #15
Nov 17th, 2007
no. i really meant even without gcc 4.3
i would use something like a std::vector< boost::any >
or a std::list< boost::variant<int,double,std::string> >
both of which are typesafe. and i would get the same functionality.
i also would not write functions that took several dozens of arguments; so even the overloaded template technique would be quite adequate for my limited needs.
Last edited by vijayan121; Nov 17th, 2007 at 9:44 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,626
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: functions with variable number of arguments

 
0
  #16
Nov 17th, 2007
Yes, I agree that is a much better solution. With vector or list there is no need for stdarg.h or for writing an infinite amount of templates.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 5
Reputation: jray344 is an unknown quantity at this point 
Solved Threads: 0
jray344 jray344 is offline Offline
Newbie Poster

Re: functions with variable number of arguments

 
0
  #17
Nov 17th, 2007
Thanks much for all the help, I figured it out with all the suggestions
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC