c++ varargs/variadic function with two types of arguments -
i trying implement variadic function. searched web , ended finding out examples handle 1 type of arguments (for example calculating average of many integers). im case argument type not fixed. can either involve char*, int or both @ same time. here code ended :
void insertinto(int dummy, ... ) { int = dummy; va_list marker; va_start( marker, dummy ); /* initialize variable arguments. */ while( != -1 ) { cout<<"arg "<<i<<endl; /* or c here */ = va_arg( marker, int); //c = va_arg( marker, char*); } va_end( marker ); /* reset variable arguments. */
now work okay if had deal integers see have char* c variable in comments use in case argument char*.
so question is, how handle returned value of va_arg without knowing if int or char* ?
since you're doing c++ there's no need use untyped c-style variadic function.
you can define chainable method like
class inserter { public: inserter& operator()( char const* s ) { cout << s << endl; return *this; } inserter& operator()( int ) { cout << << endl; return *this; } };
then use like
inserter()( "blah" )( 42 )( "duh" )
variant templated insert operation commonly used building strings.
cheers & hth.,
Comments
Post a Comment