944,210 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 10602
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 15th, 2007
0

functions with variable number of arguments

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jray344 is offline Offline
5 posts
since Nov 2007
Nov 15th, 2007
0

Re: functions with variable number of arguments

see this thread that was posted only a couple hours ago.
Last edited by Ancient Dragon; Nov 15th, 2007 at 10:48 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,966 posts
since Aug 2005
Nov 16th, 2007
0

Re: functions with 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.
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 &.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Nov 16th, 2007
0

Re: functions with 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

for a small number of arguments (upto about seven or so), you could use overloaded function names.
c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Nov 16th, 2007
0

Re: functions with variable number of arguments

Click to Expand / Collapse  Quote originally posted by vijayan121 ...
> 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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,966 posts
since Aug 2005
Nov 16th, 2007
0

Re: functions with variable number of arguments

You should be glad he didn't use the boost libraries. lolz
Last edited by iamthwee; Nov 16th, 2007 at 9:46 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 16th, 2007
0

Re: functions with variable number of arguments

> 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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Nov 16th, 2007
0

Re: functions with variable number of arguments

> 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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Nov 16th, 2007
0

Re: functions with variable number of arguments

Click to Expand / Collapse  Quote originally posted by vijayan121 ...
> 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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,966 posts
since Aug 2005
Nov 16th, 2007
0

Re: functions with variable number of arguments

> 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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help me
Next Thread in C++ Forum Timeline: how to do a program 4 billing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC