一个关于可变参数宏的问题,求高人指点
首先,代码里的程序代码:
SEARCHTFILE SEARCHTTEXFILE SEARCHTPATHTEX SEARCHTPATHTEXD SEARCHTPORTRAIT
都是已经定义好的宏,替换的是if语句
程序中有多个for循环
代码如下:
程序代码:
for(int i=0,k;i<nos;++i) { scp(ch,fls[idx[i].fi]->pth); srp(ch,'\\','_'); k=gtlen(ch); if(k>20&&!scma(ch+k-20,SLK_DESTRUCT)) { Df(i); int coli; csk=new slk (fin,ch); SEARCHTFILE SEARCHTTEXFILE SEARCHTPATHTEX SEARCHTPATHTEXD SEARCHTPORTRAIT delete csk; break; } } for(int i=0,k;i<nos;++i) { scp(ch,fls[idx[i].fi]->pth); srp(ch,'\\','_'); k=gtlen(ch); if(k>12&&!scma(ch+k-12,SLK_ITEMDATA)) { Df(i); int coli; csk=new slk (fin,ch); SEARCHTFILE delete csk; break; } } for(int i=0,k;i<nos;++i) { scp(ch,fls[idx[i].fi]->pth); srp(ch,'\\','_'); k=gtlen(ch); if(k>12&&!scma(ch+k-12,SLK_UNITDATA)) { Df(i); int coli; csk=new slk (fin,ch); SEARCHTPATHTEX delete csk; break; } } for(int i=0,k;i<nos;++i) { scp(ch,fls[idx[i].fi]->pth); srp(ch,'\\','_'); k=gtlen(ch); if(k>10&&!scma(ch+k-10,SLK_UNITUI)) { Df(i); int coli; csk=new slk (fin,ch); SEARCHTFILE delete csk; break; } } for(int i=0,k;i<nos;++i) { scp(ch,fls[idx[i].fi]->pth); srp(ch,'\\','_'); k=gtlen(ch); if(k>11&&!scma(ch+k-11,SLK_DOODAD)) { Df(i); int coli; csk=new slk (fin,ch); SEARCHTFILE SEARCHTPATHTEX2 delete csk; break; } }
现在想用一个带参数的宏替换for语句,length替换k<11中的11,tag替换SLK_DOODAD
比如这样:
程序代码:
#define SEARCH(length,tag,...)\ for(int i=0,k;i<nos;++i)\ {\ scp(ch,fls[idx[i].fi]->pth);\ srp(ch,'\\','_');\ k=gtlen(ch);\ \ if(k>length&&!scma(ch+k-length,tag))\ {\ Df(i);\ int coli;\ csk=new slk (fin,ch);\ ##__VA_ARGS__\ delete csk;\ break;\ }\ }\
关键在
##__VA_ARGS__,如果写
SEARCH(20,SLK_DESTRUCT,SEARCHTFILE,SEARCHTTEXFILE,SEARCHTPATHTEX,SEARCHTPATHTEXD,SEARCHTPORTRAIT)
就变成了
程序代码:
for(int i=0,k;i<nos;++i) { scp(ch,fls[idx[i].fi]->pth); srp(ch,'\\','_'); k=gtlen(ch); if(k>20&&!scma(ch+k-20,SLK_DESTRUCT)) { Df(i); int coli; csk=new slk (fin,ch); SEARCHTFILE, SEARCHTTEXFILE, SEARCHTPATHTEX, SEARCHTPATHTEXD, SEARCHTPORTRAIT, delete csk; break; } }
这样是不行的,因为SEARCHTFILE后边多了个逗号
现在问题是如何把它们后面的逗号去掉,各位大虾大神帮帮忙!
[ 本帖最后由 walkingsk 于 2011-3-31 08:45 编辑 ]