忽略函数名为的define宏的参数
Ignoring the parameters for define macro with function name
今天,我听说C++程序员可以通过#define
关键字做一些魔鬼般的事情。例如,
#define private public
#define class struct
#define sizeof(x) (sizeof(x) - 1)
#define true (__LINE__ % 2)
#define pthread_mutex_lock(m) 0
我对function definition
感兴趣。所以我试着通过进行测试
CRITICAL_SECTION g_critSec;
#define InitializeCriticalSection(n, y) 0
void comparemutexwithcriticalsection() {
InitializeCriticalSection(&g_critSec);
std::cout << "Iterations: " << g_cRepeatCount << "nr";
// other codes...
}
它可以在VS2013下成功构建,以下是反汇编代码。
void comparemutexwithcriticalsection() {
00F9A710 push ebp
00F9A711 mov ebp,esp
00F9A713 sub esp,0CCh
00F9A719 push ebx
00F9A71A push esi
00F9A71B push edi
00F9A71C lea edi,[ebp-0CCh]
00F9A722 mov ecx,33h
00F9A727 mov eax,0CCCCCCCCh
00F9A72C rep stos dword ptr es:[edi]
InitializeCriticalSection(&g_critSec);
std::cout << "Iterations: " << g_cRepeatCount << "nr";
00F9A72E push 0FB66F4h
InitializeCriticalSection(&g_critSec);
#define
宏中的参数似乎被忽略了,对吗
基于以上,我尝试将define
作为0
来实现我自己的功能
#define myfunc(a) 0
void myfunc(int a)
{
cout << a << endl;
}
然而,它未能在VS2013下编译有人能帮我找出这里缺的东西吗?或者我有什么不对劲
首先让我们非常清楚,重新定义像private
这样的语言关键字是未定义的,并且可能有多种行为方式。
然后,对于您的函数,问题是在定义函数之前创建#define
。试试这个方法:
void myfunc(int a)
{
cout << a << endl;
}
#define myfunc(a) 0
如果你按照你最初提议的方式做,你会在预处理后得到这个,而这显然是非法的:
void 0(int a)
{
cout << a << endl;
}
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何将定义(#define some_define)作为函数的参数传递
- #define 参数字符串化,但宽字符串 L " instead of "
- 在 #define 中使用模板参数类型
- 井号 #define 函数参数
- 使用 define 作为定义 C(++) 的参数
- #Define & lpCmdLine 参数
- 一元 '*' 的 #define 和无效类型参数(具有 'double')
- #define 函数参数 C++
- Fortran 的参数语句是否与 C/C++ 的 #define 语句相似?
- 宏的参数是否在给定 #define(void)0)的情况下执行?
- 忽略函数名为的define宏的参数
- c++用#define设置默认参数
- 预处理器 #define 带参数,但没有函数体
- 尽管gcc -E输出正确,但#define宏中的无参数函数会导致错误