宏重新定义 [英] Macro redefinition
问题描述
我正在实现一个解析器,其中定义了很多不同的数据
元素。现在不是手工编码X定义,而是表初始化所需的大量行数,我决定生成
这个来源。
所以,我基本上使用了这个技巧:
void gen_source_function(FILE * out)
{
#define EXPAND_DEF( NUM,名称)generate_source_1(下,NUM,#NAME)
的#include" iso8583_defs"
的#define EXPAND_DEF(NUM,名称)generate_source_2(下,num,#name)
#include" iso8583_defs"
}
其中iso8583_defs ;确实包含了很多这样的行:
EXPAND_DEF(1,FIELD_NAME);
EXPAND_DEF(2,ANOTHER_FIELD_NAME);
我的问题很简单,上面的C代码是否允许?
-
Tor< torust [at] online [dot] no>
I''m implementing a parser, where there is a lot of different data
elements defined. Now instead of hand-coding X defines, and the massive
number of lines needed for table initialization, I decided to generate
this source instead.
So, I basically used this trick:
void gen_source_function(FILE *out)
{
#define EXPAND_DEF(num, name) generate_source_1(out, num, #name)
#include "iso8583_defs"
#define EXPAND_DEF(num, name) generate_source_2(out, num, #name)
#include "iso8583_defs"
}
where the "iso8583_defs" file, did contain a lot of lines like this:
EXPAND_DEF(1, FIELD_NAME);
EXPAND_DEF(2, ANOTHER_FIELD_NAME);
My question is simply, is the above C code allowed?
--
Tor <torust [at] online [dot] no>
推荐答案
Tor Rustad< to ******** @ hotmail.comwrites:
Tor Rustad <to********@hotmail.comwrites:
void gen_source_function(FILE * out)
{
#define EXPAND_DEF(num,name)generate_source_1(out,num,#name)
的#include" iso8583_defs"
的#define EXPAND_DEF(NUM,名称)generate_source_2(下,NUM,#NAME)
的#include" iso8583_defs" < br $>
}
其中iso8583_defs确实包含了很多这样的行:
EXPAND_DEF(1,FIELD_NAME);
EXPAND_DEF(2,ANOTHER_FIELD_NAME);
我的问题很简单,是否允许上述C代码?
void gen_source_function(FILE *out)
{
#define EXPAND_DEF(num, name) generate_source_1(out, num, #name)
#include "iso8583_defs"
#define EXPAND_DEF(num, name) generate_source_2(out, num, #name)
#include "iso8583_defs"
}
where the "iso8583_defs" file, did contain a lot of lines like this:
EXPAND_DEF(1, FIELD_NAME);
EXPAND_DEF(2, ANOTHER_FIELD_NAME);
My question is simply, is the above C code allowed?
当然。很多代码都使用这个技巧来避免冗余。
如果你不喜欢使用包含文件的想法,那你
也可以使用宏:
void gen_source_function(FILE * out)
{
#define ISO8583_DEFS \
EXPAND_DEF(1,FIELD_NAME); \
EXPAND_DEF(2,ANOTHER_FIELD_NAME);
#define EXPAND_DEF(num,name)generate_source_1(out,num,#name)
ISO8583_DEFS
#define EXPAND_DEF(num,name)generate_source_2(out,num,#name)
ISO8583_DEFS
}
我认为这是一个更好的折腾。两者都不漂亮,
都有效。
-
char a [] =" \ n .CJacehknorstu" ;; int putchar (INT); INT主(无效){无符号长b []
= {0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},* p
= b,i = 24; for(; p + =!* p; * p / = 4)switch(0 [p]& 3)case 0:{return 0; for(p - ; i--; i - )case +
2:{i ++; if(i)break; else default:continue; if(0)case 1:putchar(a [i& 15]); break;}} }
Sure. Lots of code uses this trick to avoid redundancy.
If you don''t like the idea of using an include file for this, you
can also use a macro:
void gen_source_function(FILE *out)
{
#define ISO8583_DEFS \
EXPAND_DEF(1, FIELD_NAME); \
EXPAND_DEF(2, ANOTHER_FIELD_NAME);
#define EXPAND_DEF(num, name) generate_source_1(out, num, #name)
ISO8583_DEFS
#define EXPAND_DEF(num, name) generate_source_2(out, num, #name)
ISO8583_DEFS
}
I think it''s a toss-up which is better. Neither is pretty, both
are functional.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
8月31日上午11点04分,Tor Rustad< tor_rus ... @ hotmail.comwrote:
On Aug 31, 11:04 am, Tor Rustad <tor_rus...@hotmail.comwrote:
我正在实现一个解析器,其中定义了很多不同的数据
元素。现在不是手工编码X定义,而是表初始化所需的大量行数,我决定生成
这个来源。
所以,我基本上使用了这个技巧:
void gen_source_function(FILE * out)
{
的#define EXPAND_DEF(NUM,名称)generate_source_1(下,NUM,#NAME)
的#include" iso8583_defs"
的#define EXPAND_DEF(NUM,名称)generate_source_2(out,num,#name)
#include" iso8583_defs"
}
where iso8583_defs确实包含了很多这样的行:
EXPAND_DEF(1,FIELD_NAME);
EXPAND_DEF(2,ANOTHER_FIELD_NAME);
我的问题很简单,上面的C代码是允许的吗?
I''m implementing a parser, where there is a lot of different data
elements defined. Now instead of hand-coding X defines, and the massive
number of lines needed for table initialization, I decided to generate
this source instead.
So, I basically used this trick:
void gen_source_function(FILE *out)
{
#define EXPAND_DEF(num, name) generate_source_1(out, num, #name)
#include "iso8583_defs"
#define EXPAND_DEF(num, name) generate_source_2(out, num, #name)
#include "iso8583_defs"
}
where the "iso8583_defs" file, did contain a lot of lines like this:
EXPAND_DEF(1, FIELD_NAME);
EXPAND_DEF(2, ANOTHER_FIELD_NAME);
My question is simply, is the above C code allowed?
这是我工作的代码中非常常见的习语。一个警告:不要在重新定义之前忘记#undef EXPAND_DEF。
问候,
- = Dave
It''s a pretty common idiom in the code I work with. One caveat: don''t
forget to #undef EXPAND_DEF before redefining it.
Regards,
-=Dave
Ben Pfaff写道:
Ben Pfaff wrote:
Tor Rustad< to *** *****@hotmail.comwrites:
Tor Rustad <to********@hotmail.comwrites:
> void gen_source_function(FILE * out)
{
#define EXPAND_DEF(num, name)generate_source_1(out,num,#name)
#include" iso8583_defs"
#define EXPAND_DEF(num,name)generate_source_2(out,num,#name)
#include" iso8583_defs"
其中iso8583_defs文件,确实包含很多这样的行:
EXPAND_DEF(1,FIELD_NAME);
EXPAND_DEF(2,ANOTHER_FIELD_NAME);
我的问题很简单,允许上面的C代码?
>void gen_source_function(FILE *out)
{
#define EXPAND_DEF(num, name) generate_source_1(out, num, #name)
#include "iso8583_defs"
#define EXPAND_DEF(num, name) generate_source_2(out, num, #name)
#include "iso8583_defs"
}
where the "iso8583_defs" file, did contain a lot of lines like this:
EXPAND_DEF(1, FIELD_NAME);
EXPAND_DEF(2, ANOTHER_FIELD_NAME);
My question is simply, is the above C code allowed?
当然。许多代码使用此技巧来避免冗余。
Sure. Lots of code uses this trick to avoid redundancy.
是的,这就是主意。
Yup, that''s the idea.
如果你不喜欢这个主意为此使用包含文件,你
也可以使用宏:
void gen_source_function(FILE * out)
{
#define ISO8583_DEFS \
EXPAND_DEF(1,FIELD_NAME); \
EXPAND_DEF(2,ANOTHER_FIELD_NAME);
#define EXPAND_DEF(num,name)generate_source_1(out,num,#name)
ISO8583_DEFS
#define EXPAND_DEF(num,name)generate_source_2(out,num,#name)
ISO8583_DEFS
}
If you don''t like the idea of using an include file for this, you
can also use a macro:
void gen_source_function(FILE *out)
{
#define ISO8583_DEFS \
EXPAND_DEF(1, FIELD_NAME); \
EXPAND_DEF(2, ANOTHER_FIELD_NAME);
#define EXPAND_DEF(num, name) generate_source_1(out, num, #name)
ISO8583_DEFS
#define EXPAND_DEF(num, name) generate_source_2(out, num, #name)
ISO8583_DEFS
}
有趣的是,我之前没有见过这个解决方案。
但是,有一个翻译限制逻辑
源代码行中的509个字符,并且在拼接之后不是逻辑源代码行
您的ISO8583_DEFS在上面?
-
Tor< torust [at] online [dot] no>
Interesting, I haven''t seen this solution before.
However, there is a translation limit of "509 characters in a logical
source line", and isn''t logical source lines made up after line-splicing
your ISO8583_DEFS above?
--
Tor <torust [at] online [dot] no>
这篇关于宏重新定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!