以下代码-

#define test_macro (some_typename) some_typename func (some_typename x) {return x;}
test_macro (int)

无法使用g++ 4.4.7进行编译,并出现以下错误-
constructor, destructor, or type conversion before 'some_typename'
unqualified-id before 'int'
')' before 'int'

我要去哪里错了?

最佳答案

宏不知道类型名或任何其他语言功能,因为它们是预处理器的一部分。您的代码唯一的问题是格式,即一些额外的空格:

#define test_macro(some_typename) some_typename func (some_typename x) {return x;}
test_macro(int)

附带说明一下,当您需要类型名作为参数时,请考虑使用模板-它们很擅长:)

10-08 04:58