| | |
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:
Solved Threads: 164
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.
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.
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> // compile with gcc 4.3 with the -std=c++0x switch template< typename T > inline const T& largest_of( const T& a, const T& b ) { return a>b ? a : b ; } template< typename T, typename ...REST > inline const T& largest_of( const T& first, const REST&... rest... ) { return largest_of( first, largest_of( rest... ) ) ; } int main() { std::cout << largest_of( 1, 9, 2, 8, 3, 7, 4, 6, 5 ) << '\n' ; std::cout << largest_of( 5.3, 8.9, -3.2, 17.8, 4.2, 7.0 ) << '\n' ; const std::string s1("abc"), s2("2345"), s3("vufuk"), s4("ffyfyu") ; std::cout << largest_of( s1, s2, s3, s4 ) << '\n' ; } /** >g++43 -Wall -std=c++0x -pedantic -Werror largest_of.cc && ./a.out 9 17.8 vufuk */
Last edited by vijayan121; Nov 17th, 2007 at 5:01 am.
•
•
•
•
>
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.
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> // compile with gcc 4.3 with the -std=c++0x switch template< typename T > inline const T& largest_of( const T& a, const T& b ) { return a>b ? a : b ; } template< typename T, typename ...REST > inline const T& largest_of( const T& first, const REST&... rest... ) { return largest_of( first, largest_of( rest... ) ) ; } int main() { std::cout << largest_of( 1, 9, 2, 8, 3, 7, 4, 6, 5 ) << '\n' ; std::cout << largest_of( 5.3, 8.9, -3.2, 17.8, 4.2, 7.0 ) << '\n' ; const std::string s1("abc"), s2("2345"), s3("vufuk"), s4("ffyfyu") ; std::cout << largest_of( s1, s2, s3, s4 ) << '\n' ; } /** >g++43 -Wall -std=c++0x -pedantic -Werror largest_of.cc && ./a.out 9 17.8 vufuk */
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.
•
•
•
•
> 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.
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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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.
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.
![]() |
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 |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






