有没有办法做一个#define里面另一个#define? [英] Is there a way to do a #define inside of another #define?

查看:452
本文介绍了有没有办法做一个#define里面另一个#define?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我想在腿部拍摄自己;)但是,它将允许我使其余(大量)的代码更小,更可读。

I know that I am trying to shoot myself in the leg ;) However, it will allow me to make the rest (big amount) of code smaller and more readable.

在另一个预处理器宏中创建预处理器宏有什么棘手的方法吗?

Is there any tricky way to create preprocessor macro inside of another preprocessor macro?

我正在寻找什么。我的实际情况是更复杂的

Here is the example, what I am looking for. My real scenario is more complex

// That's what I want to do and surely C++ doesn't like it.
#define MACROCREATER(B) #define MACRO##B B+B

void foo()
{
 MACROCREATOR(5) // This should create new macro (#define MACRO5 5+5)

 int a = MACRO5; // this will use new macro
}


推荐答案

C ++标准说(16.3.4.3):

The C++ Standard says (16.3.4.3):


生成的完全
宏替换预处理令牌
序列[...的宏扩展...]不作为
预处理指令处理,即使它
类似一个...

The resulting completely macro-replaced preprocessing token sequence [... of the macro expansion...] is not processed as a preprocessing directive even if it resembles one...

所以没有,没有'官方'的方式来实现你想要的宏。

So no, there is no 'official' way of achieving what you want with macros.

这篇关于有没有办法做一个#define里面另一个#define?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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