C# null coalescing operator equivalent for c++

前端 未结 7 1633
走了就别回头了
走了就别回头了 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;
    }
    
    0 讨论(0)
  • 2020-12-09 16:43

    How about this?

    #define IFNULL(a,b) ((a) == null ? (b) : (a))
    
    0 讨论(0)
  • 2020-12-09 16:44

    Here are two macros to replicate the ?. and ?? operators respectively. They also safely eval operands only once:

    #define COACALL(a, b) ([&](){ auto val = (a); if (val) (val->b); }());
    #define COALESCE(a, b) ([&](){ auto val = (a); return ((val) == NULL ? (b) : (val)); }()) 
    

    Used like so:

    COACALL( goodPtr , sayHello() );
    COACALL( nullPtr , sayHello() );
    
    COALESCE( nullPtr, goodPtr )->sayHello();
    COALESCE( nullPtr, COALESCE( nullPtr, goodPtr ) )->sayHello();
    
    COACALL( COALESCE( nullPtr, goodPtr ), sayHello() );
    
    0 讨论(0)
  • 2020-12-09 16:46

    Using templates and C++11 lambdas. The first argument (left-hand side) is only evaluated once. The second argument (right-hand side) is only evaluated if the first is false (note that 'if' and '?' statically cast the provided expression to bool, and that pointers have 'explicit operator bool() const' that is equalivent to '!= nullptr')

    template<typename TValue, typename TSpareEvaluator>
    TValue
    coalesce(TValue mainValue, TSpareEvaluator evaluateSpare) {
    
        return mainValue ? mainValue : evaluateSpare();
    }
    

    Example of use

    void * const      nonZeroPtr = reinterpret_cast<void *>(0xF);
    void * const otherNonZeroPtr = reinterpret_cast<void *>(0xA);
    
    std::cout << coalesce(nonZeroPtr, [&] () { std::cout << "Never called"; return otherNonZeroPtr; }) << "\n";
    

    Will just print '0xf' in the console. Having to write a lambda for the rhs is a little bit of boilerplate

    [&] () { return <rhs>; }
    

    but it's the best that one can do if one lacks support by the language syntax.

    0 讨论(0)
  • 2020-12-09 16:59

    I just found this: The ?? operator aka the Null Coalescing Operator

    You also have it in C/C++ as a GNU extension using the ?: operator :

    string pageTitle = getTitle() ?: "Default Title";
    
    0 讨论(0)
  • 2020-12-09 17:01

    Just want to expand @Samuel Garcia's answer by generalising the template and adding helper macros to cut down on lambda boilerplate:

    #include <utility>
    
    namespace coalesce_impl
    {
        template<typename LHS, typename RHS>
        auto coalesce(LHS lhs, RHS rhs) ->
            typename std::remove_reference<decltype(lhs())>::type&&
        {
            auto&& initialValue = lhs();
            if (initialValue)
                return std::move(initialValue);
            else
                return std::move(rhs());
        }
    
        template<typename LHS, typename RHS, typename ...RHSs>
        auto coalesce(LHS lhs, RHS rhs, RHSs ...rhss) ->
            typename std::remove_reference<decltype(lhs())>::type&&
        {
            auto&& initialValue = lhs();
            if (initialValue)
                return std::move(initialValue);
            else
                return std::move(coalesce(rhs, rhss...));
        }
    }
    
    #define COALESCE(x) (::coalesce_impl::coalesce([&](){ return ( x ); }))
    #define OR_ELSE     ); }, [&](){ return (
    

    Using the macros, you can just:

    int* f();
    int* g();
    int* h();
    
    int* x = COALESCE( f() OR_ELSE g() OR_ELSE h() );
    

    I hope this helps.

    0 讨论(0)
提交回复
热议问题