2017-02-22 65 views
1

下面的代码 -如何编写接受类型名称的宏?

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

我要去哪里错了?

+5

为什么你在使用宏吗?模板也许? –

+0

@EdHeal test_macro可能被C应用程序使用 – Curious

回答

8

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

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

在一个侧面说明,当你需要一个类型名称为参数,考虑模板 - 他们善于吧:)

+0

谢谢,解决了它! – Curious

相关问题