// Example of homogeneous variable argument lists (by cheating).
//
// The syntax is not pretty, but we can co-opt C99 anonymous variadic macros
// to fix that. Keep in mind that this is still a pretty stupid way to use
// macros.
#include <iostream>
#include <string>
#include <vector>
// This thing collects the list of arguments
template <typename T, typename A = std::allocator <T> >
struct varargls: std::vector <T, A>
{
varargls( const T& v ):
std::vector <T, A> ( 1, v )
{ }
varargls& operator , ( const T& v )
{
push_back( v );
return *this;
}
};
// This is our (rather ordinary) function
#define foo( x, ... ) FOO(( varargls <std::string> ( x ), __VA_ARGS__ ))
void FOO( varargls <std::string> args )
{
for (size_t n = 0; n < args.size(); n++)
std::cout << n << ": " << args[ n ] << "\n";
}
int main()
{
usingnamespace std;
string foo = "Yeah!"; // Notice how the pre-processor is smart enough to
// distinguish between a variable and a function.
foo( "Hello,", "world!", "", foo, "Good-bye." );
return 0;
}
Wow, thanks for that! I've only been programming in C++ for a week, so I'm having a litlte trouble following your code, but it looks like it'll do exactly what I want.