燮preSS C近拍变量替换 [英] Suppress C Macro Variable Substitution

查看:177
本文介绍了燮preSS C近拍变量替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code的此位(一个跨preTER的垃圾收集第四体系的一部分,实际上):

I have this bit of code (part of an interpreter for a garbage-collected Forth system, actually):

#define PRIMITIVE(name) \
    do \
    { \
        VocabEntry* entry = (VocabEntry*)gc_alloc(sizeof(VocabEntry)); \
        entry->code = name; \
        entry->name = cstr_to_pstr(#name); \
        entry->prev = latest_vocab_entry; \
        latest_vocab_entry = entry; \
    } \ 
    while (false)

PRIMITIVE(dup);
PRIMITIVE(drop);
PRIMITIVE(swap);
// and a lot more

但有一个问题:在该行

but there's a problem: in the line

entry->name = cstr_to_pstr(#name);

名称字段代替了 DUP 交换,其余。我想不被取代的字段名称。

the name field is substituted for dup, drop, swap, and the rest. I want the field name to not be substituted.

那么,有没有什么办法可以解决这个问题,除了简单地重命名宏参数?

So, is there any way to solve this, other than simply renaming the macro argument?

回答,请解释一下,如果有,一般来说,一个办法燮preSS宏参数名的宏体替代。不要回答只是做了这样(请)。

推荐答案

您可以定义不同的宏扩展到名称,就像这样:

You can define a different macro to expand to name, like this:

#define Name name

和修改名称本原宏使用新宏,像这样的领域:

and change the name field in the PRIMITIVE macro to use the new macro, like this:

#define PRIMITIVE(name) \
    do \
    { \
        VocabEntry* entry = (VocabEntry*)gc_alloc(sizeof(VocabEntry)); \
        entry->code = name; \
        entry->Name = cstr_to_pstr(#name); \
        entry->prev = latest_vocab_entry; \
        latest_vocab_entry = entry; \
    } \ 
    while (false)

除了使用在宏体从参数名称不同的东西或改变参数的名称,还有没有其他的方法在C语言中做到这一点。每Ç2011(N1570)6.10.3.1 1,当一个函数宏是公认的,参数名被立即取代除非 ## 为present,还有没有其他异常:

Other than using something different from the parameter name in the macro body or changing the parameter name, there is no other way to do this in the C language. Per C 2011 (N1570) 6.10.3.1 1, when a function-like macro is recognized, parameter names are immediately substituted except when # or ## is present, and there no other exceptions:

一个函数宏的调用的参数已经确定后,参数替换发生。在替换列表中的参数,除非由#或## preprocessing令牌或后跟一个## preprocessing令牌pceded $ P $(见下文),由相应的参数后更换其中的全部宏已经扩大。

After the arguments for the invocation of a function-like macro have been identified, argument substitution takes place. A parameter in the replacement list, unless preceded by a # or ## preprocessing token or followed by a ## preprocessing token (see below), is replaced by the corresponding argument after all macros contained therein have been expanded.

标记改变参数名称为一个字符串,这是在这种情况下没有用。在 ## 标记扩展参数名并将其粘贴与相邻的道理,这也是在这种情况下没有一起使用。

The # token changes the parameter name to a string, which is no use in this situation. The ## token expands the parameter name and pastes it together with an adjacent token, which is also no use in this situation.

这篇关于燮preSS C近拍变量替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆