在不带括号的宏内使用逗号:如何混合和匹配模板? [英] Using commas inside a macro without parenthesis: How can I mix and match with a template?

查看:138
本文介绍了在不带括号的宏内使用逗号:如何混合和匹配模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个简单的宏:

  #define ECHO(x)x 

ECHO (1,2))

这将产生我们期望的精确输出:

  foo(1,2)



现在考虑如果我使用一个模板而不是一个函数调用会发生什么:

  ECHO(template< int,bool>)

这会导致错误,因为预处理器解释模板 bool> 作为宏的两个单独的参数。预处理程序无法识别范围的<>



在宏中?

解决方案

  #define COMMA,
ECHO(template< int COMMA bool>)

有点痛苦,但它有效。



FWIW,如果参数的语法允许() s,则不需要替换,例如

  ECHO((a,b))


$ b b

将用于单个参数宏,但不适用于所有情况(包括您的)。


Consider a simple macro:

#define ECHO(x) x

ECHO(foo(1, 2))

This produces the exact output we expect:

foo(1, 2)

The above example works because the parenthesis adjacent to the function call are recognized by the preprocessor.

Now consider what happens if I use a template instead of a function call:

ECHO(template<int, bool>)

This causes an error because the preprocessor interprets the template<int and the bool> as two separate arguments to the macro. The preprocessor doesn't recognize <> for scope!

Is there anyway to use a template like this in a macro?

解决方案

#define COMMA ,
ECHO(template<int COMMA bool>)

A little painful, but it works.

FWIW, if the syntax for the argument allows ()s, you don't need the substitution, e.g.,

ECHO((a, b))

will work for a single argument macro but that doesn't work in all cases (including yours).

这篇关于在不带括号的宏内使用逗号:如何混合和匹配模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆