在浏览一些大量混合 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
在这个特定的上下文中有什么区别?
我不相信这是 this question 的拷贝因为它专门询问函数签名的含义,而不是更笼统的“有什么区别”。
最佳答案
一个简单的 C++ 测试程序演示了不同之处:
typedef void VOID;
void f(VOID) {}
int main()
{
f();
}
在编译时(作为 C++),it gives these error :
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) {}
^
它解释了注释在您的代码中的含义。特别是,似乎 typedef VOID
试图成为与 void
不同的类型,当它用作参数类型时 em>。
关于c++ - typedef void FOO 与 #define FOO void 在函数签名中的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29896248/