:::: www.foddex.net ::::
      Home      |      Blog      Tutorials      Reviews      |      Resume      Portfolio      |      Photos      Stuff      |      Unreal      Flame      |      Links
Foddex' head
Contact me

Stuff
NL Weather
NL Precipitation
Temperature
xkcd
ICHC
Slashdot
Kitten
wliia
TBBT - Zoom Zoom!!

BLOG: C++ variadic macros fun

posted Mon 21-04-2008 16:49:08, in the c++ (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

[ Back to blog listing ]

3 comment(s)

Keesje said at 21-04-2008 21:06

It's in the docs, RTFM, but as a true programmer , you (and I) wouldn't

http://msdn2.microsoft.com/en-us/library/ms177415(VS.80).aspx

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.

foddex said at 21-04-2008 23:27

This is helpful too:

http://gcc.gnu.org/onlinedocs/gcc/Variadic-Macros.html

Broom said at 22-04-2008 9:34

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()... :)

Leave a comment

Name (required)
URL
Copy this code1-576-018-955

Pressure building on my soul
I ask God to take control
Guide me through this fucked up world

-

I know someday you'll have a beautiful life, I know you'll be a star
In somebody else's sky, but why, why, why
Can't it be, can't it be mine