本文介紹了C# 空合并運算符等效于 C++的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
是否有 C# 空合并運算符的 C++ 等效項?我在我的代碼中做了太多的空檢查.所以一直在尋找一種方法來減少空代碼的數量.
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++ 中默認沒有辦法做到這一點,但你可以寫一個:
There isn't a way to do this by default in C++, but you could write one:
在 C# 中 ??運算符定義為
in C# the ?? operator is defined as
a ?? b === (a != null ? a : b)
所以,C++ 方法看起來像
So, the C++ method would look like
Coalesce(a, b) // put your own types in, or make a template
{
return a != null ? a : b;
}
這篇關于C# 空合并運算符等效于 C++的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!