在宏内取消字符串化 [英] Unstringize inside a Macro

查看:35
本文介绍了在宏内取消字符串化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在宏中,我可以使用字符串化运算符#:

Inside a Macro, I can use the stringizing operator #:

#define STRINGIZE(name) #name

cout << STRINGIZE(SomeClass) << endl; // Prints "SomeClass"

是否可以在宏中执行相反的 unstringize 操作?怎么样?

Is it possible to do the opposite, unstringize inside a Macro? How?

例如:

#define RUN_FUNCTION(name) UNSTRINGIZE(name)();

void myFunction {
  cout << "Hello!" << endl;
}

RUN_FUNCTION("myFunction") // Prints "Hello!"

如果没有,为什么?

推荐答案

[从评论中升级]
预处理发生在编译之前,即运行时之前.您需要在不定义自己的规则的情况下进行反思,这需要某种形式的元数据,而C ++没有.

[Promoted from comment]
Preprocessing takes place before compilation, which is before runtime. You'd need reflection to do that without defining your own rules, which requires some form of metadata, and C++ doesn't have it.

我找不到位置,但是最近我看见某个地方(也许这里,但是根据维基百科,已被推迟)这种思考可能会在将来的某个时候出现在C ++中,因此那里可能有前景.

I can't find where, but I recently saw somewhere (perhaps here, but according to Wikipedia, it's been postponed) that reflection could be coming to C++ sometime in the future, so there might be a prospect there.

这篇关于在宏内取消字符串化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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