why this PP_ARG_COUNT macro need a PP_EXPAND?

我只是一个虾纸丫 提交于 2020-01-14 06:10:14

问题


#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?


回答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.



来源:https://stackoverflow.com/questions/22754654/why-this-pp-arg-count-macro-need-a-pp-expand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!