本文介紹了C# 空合并運(yùn)算符等效于 C++的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
是否有 C# 空合并運(yùn)算符的 C++ 等效項(xiàng)?我在我的代碼中做了太多的空檢查.所以一直在尋找一種方法來(lái)減少空代碼的數(shù)量.
Is there a C++ equivalent for C# null coalescing operator? I am doing too many null checks in my code. So was looking for a way to reduce the amount of null code.
推薦答案
在 C++ 中默認(rèn)沒(méi)有辦法做到這一點(diǎn),但你可以寫(xiě)一個(gè):
There isn't a way to do this by default in C++, but you could write one:
在 C# 中 ??運(yùn)算符定義為
in C# the ?? operator is defined as
a ?? b === (a != null ? a : b)
所以,C++ 方法看起來(lái)像
So, the C++ method would look like
Coalesce(a, b) // put your own types in, or make a template
{
return a != null ? a : b;
}
這篇關(guān)于C# 空合并運(yùn)算符等效于 C++的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!