简体   繁体   English

带有不带括号的变量的C ++宏

[英]C++ macros with variables without parentheses

In C++ one can do something like 在C ++中,可以做类似的事情

#define my_happy_macro(x) do_something_with(x)

Or also 或者也

#define $(x) do_something_with(x)

What I would like to do, however, is something like 但是,我想做的是

#define $x do_something_with(x)

So that when I write 这样我写的时候

$x;

in my code, the preprocessor substitutes it with 在我的代码中,预处理器用

do_something_with(x);

Is it possible to do something like this? 有可能做这样的事情吗?

It is not something that the preprocessor can manage. 预处理程序无法管理。

As an addendum to what others have said, it is really best to avoid heavy use of macros unless you really know what you are trying to do with them, and have a good reason not to simply create a function for the job. 作为其他人所说的补充,除非您真的知道要使用宏做什么,并且有充分的理由不简单地为工作创建函数,否则最好避免大量使用宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM