除非使用双括号,为什么带有一个参数的宏不能按预期工作?

时间:2013-10-15 05:20:08

标签: c++ c templates parameters macros

#include <type_traits>

using namespace std;

template<class T, class = typename enable_if<is_same<T, char>::value>::type> // OK
struct A {};

#define ENABLE_IF(expr) class = typename enable_if<expr>::type

template<class T, ENABLE_IF((is_same<T, char>::value))> // OK
struct B {};

template<class T, ENABLE_IF(is_same<T, char>::value)> // warning C4002 and error C1004
struct C {};

int main()
{}

我的编译器是VC ++ 2013 RC。

ENABLE_IF(x)无法正常工作:

  

警告C4002:宏'ENABLE_IF'致命

的实际参数太多      

错误C1004:发现意外的文件结尾

然而ENABLE_IF((x))工作正常。

为什么?

1 个答案:

答案 0 :(得分:3)

is_same<T, char>::value

中间有一个逗号,因此预处理器将其视为宏的两个参数,

is_same<T,char>::value