在查看一些将C和C ++高度混合的源代码时,我遇到了以下内容(为保护公司的工作而进行了一些修改,含义保持不变):
/*
* Typedefs of void are synonymous with the void keyword in C,
* but not in C++. In order to support the use of MY_VOID
* in place of the void keyword to specify that a function takes no
* arguments, it must be a macro rather than a typedef.
*/
#define MY_VOID void
typedef void MY_VOID
和#define MY_VOID void
在这种特定情况下有什么区别?
我不认为这是该问题的重复部分,因为它专门询问有关函数签名的含义,而不是更笼统的“有什么区别”。
C ++中的一个简单测试程序演示了区别:
typedef void VOID;
void f(VOID) {}
int main()
{
f();
}
在编译时(作为C ++),它给出以下错误:
prog.cpp:5:8: error: '<anonymous>' has incomplete type
void f(VOID) {}
^
prog.cpp:5:12: error: invalid use of 'VOID {aka void}'
void f(VOID) {}
^
prog.cpp: In function 'int main()':
prog.cpp:9:7: error: too few arguments to function 'void f(<type error>)'
f();
^
prog.cpp:5:6: note: declared here
void f(VOID) {}
^
解释了注释在代码中的含义。特别地,似乎的typedefVOID
尝试是一种类型的不同从void
,当它被用作参数类型。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句