本文介紹了如何在 C++ 中實現無操作宏(或模板)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
如何在 C++ 中實現 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.
推薦答案
如前所述 - 沒有.
此外,您的代碼中存在印刷錯誤.
它應該是 #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] 結構以提高穩(wěn)健性.
Have fun coding! added the [do] construct for robustness as suggested in another answer.
這篇關于如何在 C++ 中實現無操作宏(或模板)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!