本文介绍了嵌套宏扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我不确定这是否是正确的术语,但是我的问题是,宏调用("PLUGIN_NAME")作为另一个宏调用("IMPLEMENT_MODULE")的参数,该宏调用随后将其打印为字符串,将该参数打印为宏调用("somePLUGIN_NAME"),而不是扩展结果("someSomePluginName").

I'm not sure if that's the right terminology to use, however my problem is that the a macro call ("PLUGIN_NAME") as a parameter to another macro call ("IMPLEMENT_MODULE"), which in turn prints it as a string, prints that argument as the macro call ("somePLUGIN_NAME") rather than the expanded result ("someSomePluginName").

请注意,"IMPLEMENT_MODULE"是API调用,因此我无法更改.

Note that "IMPLEMENT_MODULE" is an API call so I can't change that.

#define IMPLEMENT_MODULE(name) something##name

#define PLUGIN_NAME SomePluginName
#define _STR(s) #s
#define STR(s) _STR(s)
#define PLUGIN_NAME_STR STR(PLUGIN_NAME)

int main()
{
    string expected = "somethingSomePluginName";
    string actual = STR(IMPLEMENT_MODULE(PLUGIN_NAME));

    printf("expected: %s || actual: %s\n", expected.c_str(), actual.c_str());
    assert(expected == actual);
}

我把它放在这里: http://codepad.org/FRzChJtD

推荐答案

在宏扩展预处理器令牌之后,您需要另一个帮助程序宏来连接预处理器令牌:

You need another helper macro to concatenate the preprocessor tokens after macro-expanding them:

#define IMPLEMENT_MODULE_2(A, B) A##B
#define IMPLEMENT_MODULE(name) IMPLEMENT_MODULE_2(something, name)

在此处查看有效的示例

这种技术解释是,如果预处理器找到了令牌粘贴(##)或字符串化运算符(#),则宏扩展将不会发生 .

This technical explanation is that macro expansion will not occur if the token-pasting (##) or stringizing operator (#) are found by the preprocessor.

这篇关于嵌套宏扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:28