* (e-mail address removed):
Does anybody know anything about type-safe implementation of functions
with varying number of arguments?
I know one old solution - elipsis (stdarg,h). But it is NOT type-safe!
It all depends.
Consider std::cout. It doesn't use any magic (well, it does, for
initialization, but not for argument passing!). Yet it supports an
arbitrary number of arguments of different types.
First, the most basic, simply overload a function:
void foo() {}
void foo( int ) {}
void foo( int, std::string ) {}
and so on.
Second, supporting an unlimited number of arguments of the same type,
void foo( FooArgs const& args ) {}
where FooArgs is a class like (add access restrictions as appropriate)
struct FooArgs
{
FooArgs& operator()( int aValue )
{
myValues.push_back( aValue );
return *this;
}
std::vector< SharedPtr< Value const* > > values;
};
so that you could call foo like
foo( FooArgs()( 1 )( 2 )( 3 ) );
or you might use the comma operator or e.g. the << operator instead of
the function call operator.
That's the basic idea of chaining calls, letting each operator call
return a reference to the object so that the same or some other operator
can be applied to the result again, which is used by std::cout.
Third, for just transferring an arbitrary bunch of arguments to some
other function (especially a constructor), nothing beats a macro:
#define FOO( args ) SomeOtherThing args
which you'd call like
FOO( (1, 2, 3) );
And there are other techniques.
It all depends.