| | |
functions with variable number of arguments
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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.
> 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 &.
> 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 &.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> 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.
for a small number of arguments (upto about seven or so), you could use overloaded function names.
c++ Syntax (Toggle Plain Text)
#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' ; }
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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> 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.
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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> 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.
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.
•
•
•
•
> 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. .
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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> 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.
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.
![]() |
Similar Threads
- Differences Between Java and C/C++ (C++)
- vb6 - RunTime error 1004 Invalid number of arguments (Visual Basic 4 / 5 / 6)
- variable arguments in C (C)
- Parameter lists for fucntion pointers?? (C)
- c vs c++ (C++)
- intercepting functions with a variable argument list (C)
- Need to know hwo to go about this (classes/accessor functions) (C++)
- accessing private data members (C++)
Other Threads in the C++ Forum
- Previous Thread: help me
- Next Thread: how to do a program 4 billing
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






