关于宏定义
#include <iostream.h>
#define if(a==1) if( (a==1)? if_1():if_0() )
void main()
{
int a,b,c,result;
if(a==1)
cout < < "main " < <endl;
};
void if_1()
{
cout < < "11111 " < <endl;
}
void if_0()
{
cout < < "22222 " < <endl;
}
本意是将代码中 if(a==1) 替换成 if( (a==1)? if_1():if_0() )
这样我可以根据程序插装算出语句覆盖率~~
但系统报错
error C2010: '= ' : unexpected in macro formal parameter list
error C2010: '1 ' : unexpected in macro formal parameter list
error C2065: 'if_1 ' : undeclared identifier
error C2373: 'if_1 ' : redefinition; different type modifiers
看错误的提示好象是宏定义有点问题....
请问是什么原因哦?谢谢
[解决办法]
#include <iostream>
using namespace std;
#define test() ({\
(a==1)?if_1():if_0();})
void if_1()
{
cout < < "11111 " < <endl;
}
void if_0()
{
cout < < "22222 " < <endl;
}
int main()
{
int a,b,c,result;
//if(a==1)
a=2;
test();
cout < < "main " < <endl;
system( "pause ");
}
----------------------------
改了下
gcc下ok
[解决办法]
From newtypebao():
可是gcc里连可变参数的都能用宏替换....这个不行?
=====================================================
C/C++的宏替换只有两种方式:
1. Object-like Macros
例如: #define BUFFER_SIZE 1024
2. Function-like macros
例如: #define min(X, Y) ((X) < (Y) ? (X) : (Y))
不管哪种方式,紧跟在 "#define "后面的字符串(对第二种方式,括号之前的部分)必须是一个C的合法标识符(i.e.以字母或_开始,只包含字母,数字或_).