簡體   English   中英

C ++預處理器決策

[英]C++ Preprocessor Decisions

對不起我知道這是基本的,但也許它不存在或者我沒有用Google搜索正確的單詞。 有沒有, if not (是ifndef ?)一個AND和一個OR所以我可以這樣做:

if not DEBUG and MACOS

我認為像#if !defined(DEBUG) && defined(MACOS)這樣的東西應該這樣做。

#ifndef and #if做不同的事情,所以這取決於你想要什么。 如果沒有與以下名稱匹配的已定義預處理程序符號,則#ifndef為true。 當以下預處理程序表達式求值為非零時, #if為true。

您可以使用標准的&&和|| 運營商。

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

測試,如果定義了這些宏/值(甚至設置為0表示已定義)。 省略“defined()”並再次測試一個值,具體取決於您的宏

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

要么

#if !DEBUG & !MACROS

取決於你在尋找什么。 defined()也可以提供幫助

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

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

要么

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

取決於您要評估的內容。

#if#else#endif是通用的。 使用#define聲明和#undef聲明為undeclare。 使用#ifdef檢查是否聲明, #ifndef檢查,如果未聲明。

例:

#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

查看Boost預處理庫。 它可以使用預處理器完成大量任務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM