本文介紹了如何在 C++ 中實(shí)現(xiàn)無操作宏(或模板)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時送ChatGPT賬號..
如何在 C++ 中實(shí)現(xiàn) no-op 宏?
How do I implement no-op macro in C++?
#include <iostream>
#ifdef NOOP
#define conditional_noop(x) what goes here?
#else
#define conditional_noop(x) std::cout << (x)
#endif
int main() {
conditional_noop(123);
}
我希望在定義 NOOP 時不執(zhí)行任何操作,并在未定義 NOOP 時打印123".
I want this to do nothing when NOOP is defined and print "123", when NOOP is not defined.
推薦答案
如前所述 - 沒有.
此外,您的代碼中存在印刷錯誤.
它應(yīng)該是 #else 而不是 #elif.如果是#elif,則后面跟著新的條件
As mentioned before - nothing.
Also, there is a misprint in your code.
it should be #else not #elif. if it is #elif it is to be followed by the new condition
#include <iostream>
#ifdef NOOP
#define conditional_noop(x) do {} while(0)
#else
#define conditional_noop(x) std::cout << (x)
#endif
玩得開心編碼!按照另一個答案中的建議添加了 [do] 結(jié)構(gòu)以提高穩(wěn)健性.
Have fun coding! added the [do] construct for robustness as suggested in another answer.
這篇關(guān)于如何在 C++ 中實(shí)現(xiàn)無操作宏(或模板)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!