简体   繁体   English

C ++预处理器决策

[英]C++ Preprocessor Decisions

Sorry I know this is basic, but perhaps it doesn't exist or I'm not googling the right words. 对不起我知道这是基本的,但也许它不存在或者我没有用Google搜索正确的单词。 Is there and an if not (is that ifndef ?) an AND and an OR so I could do something like: 有没有, if not (是ifndef ?)一个AND和一个OR所以我可以这样做:

if not DEBUG and MACOS

我认为像#if !defined(DEBUG) && defined(MACOS)这样的东西应该这样做。

#ifndef and #if do different things so it depends on what you want. #ifndef and #if做不同的事情,所以这取决于你想要什么。 #ifndef is true when there is no defined preprocessor symbol that matches the name following. 如果没有与以下名称匹配的已定义预处理程序符号,则#ifndef为true。 #if is true when the following preprocessor expression evaluates to non-zero. 当以下预处理程序表达式求值为非零时, #if为true。

You can use the standard && and || 您可以使用标准的&&和|| operators. 运营商。

#if !defined(DEBUG) && defined(MACOS)
#error "Ouch!"
#endif

tests, if those macros/values are defined (even set to 0 means defined). 测试,如果定义了这些宏/值(甚至设置为0表示已定义)。 Leave out the "defined()" and test again a value, depending on your macros, like 省略“defined()”并再次测试一个值,具体取决于您的宏

#if DEBUG==0 && MACOS==1
#error "Spam!"
#endif
#if !DEBUG && MACROS

or 要么

#if !DEBUG & !MACROS

depending on what you are looking for. 取决于你在寻找什么。 defined() can also help defined()也可以提供帮助

#if !defined(DEBUG) && defined(MACROS)

#if !(defined(DEBUG) && defined(MACOS))

or 要么

#if !defined(DEBUG) && !defined(MACOS)

depending on what you're trying to evaluate. 取决于您要评估的内容。

#if , #else and #endif are general. #if#else#endif是通用的。 Use #define to declare and #undef to undeclare. 使用#define声明和#undef声明为undeclare。 Use #ifdef to check if is declared and #ifndef to check, if is not declared. 使用#ifdef检查是否声明, #ifndef检查,如果未声明。

Example: 例:

#ifndef LABEL
#define LABEL some_value // declares LABEL as some_value
#else
#undef LABEL // undeclare previously declared LABEL...
#define LABEL new_value // to declare a new_value
#endif

Check out the Boost preprocessing library. 查看Boost预处理库。 It can accomplish a large number of tasks using the preprocessor. 它可以使用预处理器完成大量任务。

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

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