Curious MSVC Variadic Macros
The Most Weird Macro Expansion
This is a short post, so let’s go straight to the point.
I was developing a small cross-platform C++ library, in which I used a trick to allow a macro to take arbitrary number of arguments. The macro acts as a template to generate function signature, which essentially concatenates the arguments into a single string. Below is a simplified version of the macro:
1 |
|
It works perfectly with GCC and Clang on Linux and macOS. That is to say, you should expect it to generate the following function signature:
1 | void foo(); |
However, when I tried to compile it with MSVC, I was shocked to see the result of the macro expansion. The macro was expanded as if all arguments, including the comma, were treated as a single argument! Instead of the expected result, you would see the following function signature signature, which is insane:
1 | void foo(); |
What the?
Thanks to Visual Studio’s visualize macro expansion feature, I was able to see the macro expansion step-by-step.
1 | DECLARE(ns, foo, bar) |
By looking at the expansion process carefully, it seems __VA_ARGS__ is treated as a single argument instead of a list of arguments, and it even contains the comma…
The Macro Expansion Process
Macro expansion is done during the preprocessing stage of C/C++ compilation. Hopefully you know something about the macros in C/C++. Here is an open-access C standard document you may refer to: ISO C Standard (Committee Draft). On page 151 (PDF page 163) it describes the macro replacement mechanism in detail, and here is its explanation of __VA_ARGS__:
“An identifier
__VA_ARGS__that occurs in the replacement list shall be treated as if it were a parameter, and the variable arguments shall form the preprocessing tokens used to replace it.”
So, with this definition, it is no surprise that the preprocessor of MSVC treats ns, foo, bar as a single argument, thus eventually choose _GET_NAME_1. The reason GCC and Clang work as expected is that, they recursively expand __VA_ARGS__ automatically. You may check GCC’s Macro Expansion Algorithm for more details.
Workaround
So, how to make your macro work across different compilers, or I should say, to make it work with MSVC? Well, here is a simple workaround. Now that MSVC won’t expand __VA_ARGS__ recursively, we can force it to do so by adding an extra layer of macro expansion. In other words, whenever __VA_ARGS__ is used, we wrap the macro with an expansion macro, so that the content of __VA_ARGS__ (the list of arguments) can be correctly interpreted by the nested macro.
The key is that we force MSVC to expand __VA_ARGS__, so its content, instead of itself, can be passed to the next macro. The expansion macro can be simple, just return what ever it receives. The purpose is to add an extra layer which keeps everything but forces a expansion, so both of the two versions below work:
1 |
And now, with this expand macro, we can modify the original macro to make it work with MSVC. Just wrap all macro replacements that contain __VA_ARGS__ with _EXPAND, and you are good to go.
1 |
|
Ta-da! 🎉 Now it works perfectly everywhere!
Epilogue
In fact I saw this kind of expand macro stuff long before, but back then I just copy the code and was totally unaware of the reason behind it. Now when I apply this trick myself, the problem finally arise. Hmm… interesting. ᓚᘏᗢ









