DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   functions with variable number of arguments (http://www.daniweb.com/forums/thread96948.html)

jray344 Nov 15th, 2007 10:41 pm
functions with variable number of arguments
 
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

Ancient Dragon Nov 15th, 2007 10:47 pm
Re: functions with variable number of arguments
 
see this thread that was posted only a couple hours ago.

Salem Nov 16th, 2007 4:29 am
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 &.

vijayan121 Nov 16th, 2007 9:31 am
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.
#include <iostream>
#include <algorithm>
template< typename T > inline
const T& max( const T& a, const T& b, const T& c )
{ return std::max( std::max(a,b), c ) ; }

template< typename T > inline
const T& max( const T& a, const T& b, const T& c, const T& d )
{ return std::max( std::max(a,b), std::max(c,d) ) ; }

template< typename T > inline
const T& max( const T& a, const T& b, const T& c,
              const T& d, const T& e )
{ return std::max( std::max(a,b), max(c,d,e) ) ; }

template< typename T > inline
const T& max( const T& a, const T& b, const T& c,
              const T& d, const T& e, const T& f )
{ return std::max( max(a,b,c), max(d,e,f) ) ; }

template< typename T > inline
const T& max( const T& a, const T& b, const T& c, const T& d,
              const T& e, const T& f, const T& g )
{ return std::max( max(a,b,c,d), max(e,f,g) ) ; }

template< typename T > inline
const T& max( const T& a, const T& b, const T& c, const T& d,
              const T& e, const T& f, const T& g, const T& h )
{ return std::max( max(a,b,c,d), max(e,f,g,h) ) ; }

int main()
{
  std::cout << max( 12.3, 4.5, 67.8, 9.98, 76.5, 4.3, 2.1, 0.0 ) << '\n' ;
}

Ancient Dragon Nov 16th, 2007 9:38 am
Re: functions with variable number of arguments
 
Quote:

Originally Posted by vijayan121 (Post 471221)
> 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.

iamthwee Nov 16th, 2007 9:46 am
Re: functions with variable number of arguments
 
You should be glad he didn't use the boost libraries. lolz

vijayan121 Nov 16th, 2007 9:56 am
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.

vijayan121 Nov 16th, 2007 10:04 am
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.

Ancient Dragon Nov 16th, 2007 10:25 am
Re: functions with variable number of arguments
 
Quote:

Originally Posted by vijayan121 (Post 471236)
> 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.

vijayan121 Nov 16th, 2007 1:52 pm
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.


All times are GMT -4. The time now is 8:27 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC