简体   繁体   English

如何在C ++中将#define是否像python一样

[英]How to #define if in C++ to be python-like

I know that I can define something like 我知道我可以定义类似

#define LESS(x, y) if(x < y)

so I wonder can I define if in c++ to be python-like template or not? 所以我想知道我if在c ++中定义if为类似python的模板? such as

if x < y: do something instead of if(x < y) do something if x < y: do something而不是if(x < y) do something

To put it shortly - no . 简而言之-

from cplusplus.com : 来自cplusplus.com

When the preprocessor encounters this directive, it replaces any occurrence of identifier in the rest of the code by replacement. 当预处理器遇到此指令时,它将通过替换替换代码其余部分中所有出现的标识符。 This replacement can be an expression, a statement, a block or simply anything. 该替换可以是表达式,语句,块或任何其他内容。 The preprocessor does not understand C++, it simply replaces any occurrence of identifier by replacement. 预处理器不了解C ++,它只是通过替换来替换出现的任何标识符。

What you want is something like this 你想要的是这样的

#define if(x):(y) if(x){##y##};

Nevertheless, colon is a keyword and can't be used for identifier, so it breaks your macro definition. 但是,冒号是一个关键字,不能用作标识符,因此它破坏了您的宏定义。 There's no way to escape it. 没有办法逃脱它。

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

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