C# null coalescing operator equivalent for c++

前端 未结 7 1641
走了就别回头了
走了就别回头了 2020-12-09 16:02

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.

7条回答
  •  再見小時候
    2020-12-09 16:42

    There isn't a way to do this by default in C++, but you could write one:

    in C# the ?? operator is defined as

    a ?? b === (a != null ? a : b)
    

    So, the C++ method would look like

    Coalesce(a, b) // put your own types in, or make a template
    {
        return a != null ? a : b;
    }
    

提交回复
热议问题