0
#include <type_traits>
#include <iostream>

using namespace std;

// Expand
#define PP_EXPAND(X) X

// Counter Arguments count
#define PP_ARG_COUNT(...) PP_EXPAND( PP_ARG_POPER(__VA_ARGS__, 5, 4, 3, 2, 1, 0) )
#define PP_ARG_COUNT2(...) PP_ARG_POPER(__VA_ARGS__, 5, 4, 3, 2, 1, 0)
#define PP_ARG_POPER(_1, _2, _3, _4, _5, N, ...) N

int main()
{
    cout << PP_ARG_COUNT(1, 2, int) << endl;
    cout << PP_ARG_COUNT2(1, 2, int) << endl;
    cout << PP_ARG_POPER(1, 2, int, 5, 4, 3, 2, 1 0) << endl;

    return 0;
}

i have compile this code under visual studio 2013, it output:

3
1
3

why this macro need a PP_EXPAND, and PP_ARG_COUNT2 it not work well?

Hikari
  • 293
  • 3
  • 14

1 Answers1

1

This is a workaround for a bug in the Visual C++ preprocessor. It incorrectly fails to expand comma-delimited token sequences in some contexts.

In your PP_ARG_COUNT2, the __VA_ARGS__ is treated as a single argument when used in the invocation of PP_ARG_POPER, causing the incorrect result.

The most common workaround for this issue is to introduce an additional layer of indirection that forces the compiler to reevaluate the comma-delimited token sequence. The technique used here, with PP_ARG_COUNT invoking through EXPAND is one way to do this; I presented a variation on this technique in an answer to another question.

Community
  • 1
  • 1
James McNellis
  • 327,682
  • 71
  • 882
  • 954