A coder's home for Marc "Foddex" Oude Kotte, who used to be located in Enschede, The Netherlands, but now in Stockholm, Sweden!

C++ variadic macros fun

Originally posted at Mon 21-04-2008 16:49:08, in the c++ category.
Recently I stumbled across the concept of variadic macros in C++. It's supported in modern versions of gcc for a long time, but MSVC++ supports it only from MSVC++ 2005.

And ofcourse, things wouldn't be normal if Microsoft dealt with them differently than gcc. Check out the following source code:

    int sum( int factor, ... );
    #define SUM(...) sum(1,__VA_ARGS__,-1)
    #define SUM2(...) sum(2,__VA_ARGS__,-1)

Assume that you - for some reason - want a function that sums up a variable amount of integer values and multiply it with a certain factor. In that case the above function and defines could be of help to you. If you specify this:
    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.
However, it becomes different when you DON'T specify any parameters, i.e.:
    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! ^_^

-- Foddex

4 comment(s)

Click to write your own comment

On Mon 21-04-2008 21:06 Keesje wrote: It's in the docs, RTFM, but as a true programmer , you (and I) wouldn't


The C Standard specifies that at least one argument must be passed to the ellipsis, to ensure that the macro does not resolve to an expression with a trailing comma. The Visual C++ implementation will suppress a trailing comma if no arguments are passed to the ellipsis.
On Mon 21-04-2008 23:27 foddex wrote: This is helpful too:

On Tue 22-04-2008 09:34 Broom wrote: What is wrong with 'Thinking for you' by MS? We just have to get used to it, like we experienced in Office apps... I'm sure it is a matter of time and each MS compiler will have an integrated compiler directive solve()... :)
On Fri 14-09-2012 23:06 ARQEROS wrote: No it isn't nice, Foddex. Because of this great feature I spent all night debugging my code and finally figured out that Microsoft compiler (more specific preprocessor) always removes the most recent comma before __VA_ARGS__ even when it is inappropriate. Fortunately there is a nice workaround.

Check more here:
URL: (optional!)
Write your comment:
Answer this question to prove you're human:
Does my nickname start with an f or a v?