C++ macro with lambda argument using 2+ captured elements generates error

后端 未结 2 695
长发绾君心
长发绾君心 2021-01-19 04:59
foo(const std::function& functor) {
    ....
}

#define MACRO_EXAMPLE(functor) foo(functor)

int main() {
    int i = 0, j = 0;
    MACRO_EXAMPLE([         


        
2条回答
  •  不思量自难忘°
    2021-01-19 05:48

    As an alternative to @SingerofTheFall's answer (which fixes the problem when invoking the macro), you can also fix the problem in the macro itself, by making it variadic:

    #define MACRO_EXAMPLE(...) foo(__VA_ARGS__)
    

    This works by allowing the preprocessor to parse the lambda at , tokens into multiple arguments, but then uses all of these arguments and the separating commas again, so the net effect is what you want.

提交回复
热议问题