functions with variable number of arguments

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

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

functions with variable number of arguments

 
0
  #1
Nov 15th, 2007
I don't understand functions that accept a variable number of arguments. I want to make a program with a max function taking any number of type double and returns the greatest of them
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
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: 1464
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
  #2
Nov 15th, 2007
see this thread that was posted only a couple hours ago.
Last edited by Ancient Dragon; Nov 15th, 2007 at 10:48 pm.
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 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: functions with variable number of arguments

 
0
  #3
Nov 16th, 2007
> I want to make a program with a max function taking any number of type double
> and returns the greatest of them.
So store them in an array (or better yet, a vector) and write a function which accepts that array (or better yet, a vector).

The first thing you need to realise is that variadic functions have NO TYPE CHECKS beyond the fixed parameters. Which means that you have lots of wonderful ways of screwing up the code in many new and interesting ways. Just look on the C forum to see how many times people mess up say scanf() by forgetting an &.
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
  #4
Nov 16th, 2007
> I want to make a program with a max function taking any number of type double and returns the greatest of them

for a small number of arguments (upto about seven or so), you could use overloaded function names.
  1. #include <iostream>
  2. #include <algorithm>
  3. template< typename T > inline
  4. const T& max( const T& a, const T& b, const T& c )
  5. { return std::max( std::max(a,b), c ) ; }
  6.  
  7. template< typename T > inline
  8. const T& max( const T& a, const T& b, const T& c, const T& d )
  9. { return std::max( std::max(a,b), std::max(c,d) ) ; }
  10.  
  11. template< typename T > inline
  12. const T& max( const T& a, const T& b, const T& c,
  13. const T& d, const T& e )
  14. { return std::max( std::max(a,b), max(c,d,e) ) ; }
  15.  
  16. template< typename T > inline
  17. const T& max( const T& a, const T& b, const T& c,
  18. const T& d, const T& e, const T& f )
  19. { return std::max( max(a,b,c), max(d,e,f) ) ; }
  20.  
  21. template< typename T > inline
  22. const T& max( const T& a, const T& b, const T& c, const T& d,
  23. const T& e, const T& f, const T& g )
  24. { return std::max( max(a,b,c,d), max(e,f,g) ) ; }
  25.  
  26. template< typename T > inline
  27. const T& max( const T& a, const T& b, const T& c, const T& d,
  28. const T& e, const T& f, const T& g, const T& h )
  29. { return std::max( max(a,b,c,d), max(e,f,g,h) ) ; }
  30.  
  31. int main()
  32. {
  33. std::cout << max( 12.3, 4.5, 67.8, 9.98, 76.5, 4.3, 2.1, 0.0 ) << '\n' ;
  34. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
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: 1464
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
  #5
Nov 16th, 2007
Originally Posted by vijayan121 View Post
> I want to make a program with a max function taking any number of type double and returns the greatest of them

for a small number of arguments (upto about seven or so), you could use overloaded function names.
And just how would that help the OP create a function that takes a variable number of arguments ? At least two problems with the templates you posted: (1) none of them take a variable number of arguments, and (2) the arguments are all the same data type, which is not necessarily the case with functions that take a variable number of arguments.
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: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: functions with variable number of arguments

 
0
  #6
Nov 16th, 2007
You should be glad he didn't use the boost libraries. lolz
Last edited by iamthwee; Nov 16th, 2007 at 9:46 am.
*Voted best profile in the world*
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
  #7
Nov 16th, 2007
> just how would that help the OP create a function that takes a variable number of arguments
you can call max with anything between 2 to 8 arguments; that is a variable number in my book. in any case, the C++ standard recommends some minimum values for such things as the maximum number of arguments for a function(256), the maximum nesting level of compound statements (256) etc. compilers can exceed these, but variable number of arguments does have an upper limit, no matter what technique you use. in my example, the upper limit is 8 (a reasonable value according to me. others may have different idea of what would be a reasonable value for an upper limit on number of arguments to be passed to a function. and they can create more overloads of max if they want).

> the arguments are all the same data type
yes, the OP wanted all arguments to be double values. and finding the largest implies that all arguments must support comparisons between them.
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
  #8
Nov 16th, 2007
> You should be glad he didn't use the boost libraries. lolz
yes, you should be glad. i did considor using the boost preporocessor metaprogramming library http://www.boost.org/libs/preprocessor/doc/index.html to generate the overloaded max functions. but dropped the idea as i thought it may confuse many people.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
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: 1464
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
  #9
Nov 16th, 2007
Originally Posted by vijayan121 View Post
> just how would that help the OP create a function that takes a variable number of arguments
you can call max with anything between 2 to 8 arguments; that is a variable number in my book. .
But a very poor implementation because you had to create a template for each number of arguments -- 8 arguments = 8 templates. What if he wants 50 arguments, do you want to write 50 templates ? that's why varargs.h is a better solution -- not perfect as Salem pointed out, but workable. One function -- an infinite (almost) number of arguments, and you don't have to screw around writing millions of templates or overloaded functions.

As for boost libraries -- I don't know, but it might have a better solution.
Last edited by Ancient Dragon; Nov 16th, 2007 at 10:26 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

 
0
  #10
Nov 16th, 2007
> What if he wants 50 arguments, do you want to write 50 templates ?
no. i would strongly discourage him from trying to write functions which need more than seven or eight arguments.

> One function -- an infinite (almost) number of arguments
with the template: One function -- an infinite (almost) number of types.
and completely typesafe.

> and you don't have to screw around writing millions of templates or overloaded functions.
with the template: you don't have to screw around breaking your code each time (i suppose you would say millions of times) a new type needs to be supportd.
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC