0

So I have this macro that takes a varying number of arguments and calls another macro based upon the number of arguments

#define VA_NARGS_IMPL(_1, _2, _3, _4, N, ...) BENCHMARK##N
#define VA_NARGS(...)  VA_NARGS_IMPL(__VA_ARGS__, 4, 3, 2, 1)

#define BENCHMARK(...) VA_NARGS(__VA_ARGS__)( __VA_ARGS__ )

so BENCHMARK( T, 1) becomes BENCHMARK2(T, 1) which is ...

#define BENCHMARK2( test, description ) \
  BENCHMARK_INTERNAL( test, 0, 0, description ) // NOLINT

which becomes (Only providing a snippet) ...

#define BENCHMARK_INTERNAL(test, lb, ub, d)                                   \
  struct test : public Test {                                                 \
    public:    ...                                                            \

Clang is evaluating the MACROS as I would expect, here is a snippet from that expansion ...

struct T : public Test { public: ...

but when windows preprocesses this file I get ...

struct T, 1 : public Test { public: ...

However, if I directly call BENCHMARK2 ... BENCHMARK2( T, 1) everything works as I would expect.

Does windows handle variadic macros differently? Is there a cross platform solution for my scenario?

I'm using MSVC's x64 compiler (version 14.28.29333)

Tyler Weiss
  • 139
  • 1
  • 15
  • Something's wrong with your problem description -- I don't see `VA_NARGS` defined, nor `BENCHMARK2` called. – Quentin Oct 06 '21 at 18:34
  • `VA_NARGS(__VA_ARGS__)` becomes BENCHMARK2. I added VA_NARGS for context – Tyler Weiss Oct 06 '21 at 18:36
  • Which compiler is the Windows compiler? I'm presently working with 3 compilers running on Windows. – Thomas Matthews Oct 06 '21 at 20:07
  • @ThomasMatthews The full path to the compiler I'm using is `C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29333\bin\Hostx64\x64\cl.exe` If there's anything outside of the binary/version/architecture I can offer up to provide context please let me know. – Tyler Weiss Oct 07 '21 at 03:00
  • With MSVC, `VA_NARGS(T, 1)` becomes `BENCHMARK1`, not `BENCHMARK2`: [demo](https://godbolt.org/z/jEbEbTEo6). – Igor Tandetnik Oct 07 '21 at 14:17

1 Answers1

0

Thanks to @Igor Tandetnik I found the solution.

My question was similar to this post - MSVC doesn't expand __VA_ARGS__ correctly

The issue was VA_NARGS not expanding correctly on windows. I fixed it by adding this compilation flag /Zc:preprocessor .

Tyler Weiss
  • 139
  • 1
  • 15