int sum( int factor, ... ); #define SUM(...) sum(1,__VA_ARGS__,-1) #define SUM2(...) sum(2,__VA_ARGS__,-1)
int s = SUM(1,2,4,8)it will be transformed into:
int s = sum(1,1,2,4,8,-1);So far so good, nothing special, done that a million times. The -1 is there to indicate the 'end of the list'. Not a very good approach, but you need to know somehow when you've encountered the last actual parameter.
int s = SUM2();In this case, you have a problem, as the above snippet will compile just fine in MSVC++2008, but gcc will bork. Gcc will expand that code to this:
int s = sum( 2, , -1 );and claim it's invalid C++. And he'd be right. However, the Microsoft compiler assumes that because you didn't specify any parameters, you probably didn't want that comma to be there in the first place, and will transform the code into:
int s = sum( 2, -1 );Well isn't that nice of the Microsoft compiler? It thinks FOR you! ^_^