含有#include指令宏定义 [英] macro definition containing #include directive

查看:197
本文介绍了含有#include指令宏定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法定义可能包含宏的#include
在它的身上指令?

Is there a way to define a macro that may contain #include directive in its body?

如果我只是把
的#include ,它给误差

If I just put the "#include", it gives error

C2162: "expected macro formal

参数

因为在这里我没有使用连接字符串。结果
如果我使用 \\#包含,然后我收到以下两个错误:

since here I am not using # to concatenate strings.
If I use "\# include", then I receive the following two errors:

error C2017: illegal escape sequence
error C2121: '#' : invalid character : possibly the result of a macro expansion

任何帮助吗?

推荐答案

所以像别人说,不,你不能有一个宏内部#include语句,因为preprocessor只做一通。然而,你可以使preprocessor基本上做同样的事情,用粗糙的把戏,我发现自己最近使用

So like the others say, no, you can't have #include statements inside a macro, since the preprocessor only does one pass. However, you can make the preprocessor do basically the same thing with a gnarly trick I found myself using recently.

要意识到了preprocessor指令什么都不会做一个宏里面,但是他们会做在一个文件中的东西。所以,你可以坚持$ C $的C座要变异成一个文件,像宏定义想到它(与可由其他宏改变件),然后在#包括各种此伪宏文件地方(请确保它没有包括守卫!)。它不完全一样的宏会的,但它可以实现一些pretty宏观样的结果,因为基本的#include转储只是一个文件的内容到另一个。

Realise that preprocessor directives won't do anything inside a macro, however they WILL do something in a file. So, you can stick a block of code you want to mutate into a file, thinking of it like a macro definition (with pieces that can be altered by other macros), and then #include this pseudo-macro file in various places (make sure it has no include guards!). It doesn't behave exactly like a macro would, but it can achieve some pretty macro-like results, since #include basically just dumps the contents of one file into another.

例如,考虑包括大量的进来团体名称相似的头。这是乏味它们全部写出来,或者甚至它们是自动生成的。你可以做这样的事情部分自动化将其列入:

For example, consider including lots of similarly named headers that come in groups. It is tedious to write them all out, or perhaps even they are auto-generated. You can partially automate their inclusion by doing something like this:

辅助宏头:

/* tools.hpp */

#ifndef __TOOLS_HPP__
#def __TOOLS_HPP__

// Macro for adding quotes
#define STRINGIFY(X) STRINGIFY2(X)    
#define STRINGIFY2(X) #X

// Macros for concatenating tokens
#define CAT(X,Y) CAT2(X,Y)
#define CAT2(X,Y) X##Y
#define CAT_2 CAT
#define CAT_3(X,Y,Z) CAT(X,CAT(Y,Z))
#define CAT_4(A,X,Y,Z) CAT(A,CAT_3(X,Y,Z))
// etc...

#endif

伪宏文件

/* pseudomacro.hpp */

#include "tools.hpp"
// NO INCLUDE GUARD ON PURPOSE
// Note especially FOO, which we can #define before #include-ing this file,
// in order to alter which files it will in turn #include.
// FOO fulfils the role of "parameter" in this pseudo-macro.

#define INCLUDE_FILE(HEAD,TAIL) STRINGIFY( CAT_3(HEAD,FOO,TAIL) )

#include INCLUDE_FILE(head1,tail1.hpp) // expands to #head1FOOtail1.hpp
#include INCLUDE_FILE(head2,tail2.hpp)
#include INCLUDE_FILE(head3,tail3.hpp)
#include INCLUDE_FILE(head4,tail4.hpp)
// etc..

#undef INCLUDE_FILE

源文件

/* mainfile.cpp */

// Here we automate the including of groups of similarly named files

#define FOO _groupA_
#include "pseudomacro.hpp"
// "expands" to: 
// #include "head1_groupA_tail1.hpp"
// #include "head2_groupA_tail2.hpp"
// #include "head3_groupA_tail3.hpp"
// #include "head4_groupA_tail4.hpp"
#undef FOO

#define FOO _groupB_
#include "pseudomacro.hpp"
// "expands" to: 
// #include "head1_groupB_tail1.hpp"
// #include "head2_groupB_tail2.hpp"
// #include "head3_groupB_tail3.hpp"
// #include "head4_groupB_tail4.hpp"
#undef FOO

#define FOO _groupC_
#include "pseudomacro.hpp"
#undef FOO

// etc.

这包括甚至可以是在codeS块要重复(与FOO改变)中间,由冰剑,答案要求:<一href=\"http://stackoverflow.com/questions/266501/macro-definition-containing-include-directive/266647#266647\">macro定义包含#include指令

我没有使用过这招广泛,但它得到我的工作完成。它可明显延长根据需要有尽可能多的参数,你可以运行任何preprocessor命令,你在那里想,加产生实际code。你不能用它创建作为输入的东西到另一个宏,比如你可以用正常的宏,因为你不能坚持宏里面包括。但它可以在另一个伪宏:)

I haven't used this trick extensively, but it gets my job done. It can obviously be extended to have as many "parameters" as needed, and you can run whatever preprocessor commands you like in there, plus generate actual code. You just can't use the stuff it creates as the input into another macro, like you can with normal macros, since you can't stick the include inside a macro. But it can go inside another pseudo-macro :).

其他人可能有其他限制一些意见,什么可能出错:)。

Others might have some comments on other limitations, and what could go wrong :).

这篇关于含有#include指令宏定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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