Search code examples
c++visual-c++c++17if-constexpr

if constexpr and C4702 (and C4100, and C4715)


Is there a way to fix the following problem:

This code produces a C4702 warning 'unreachable code' (on VC++ 15.8 with /std:c++17)

template <typename T, typename VariantType>
inline bool MatchMonostate( VariantType& variant )
{
    SUPPRESS_C4100( variant );
    if constexpr ( std::is_same_v<T, std::monostate> )
    {
        variant = std::monostate();
        return true;
    }
    return false;  // !!! unreachable if the above is true !!! => C4702
}

to suppress the C4100 'unreferenced formal parameter' warning, I'm already using the trick

#define SUPPRESS_C4100(x) ((void)x)

The simple idea of adding

    else
    {
        return false;
    }

results in warning C4715 'not all control paths return a value' instead.


Solution

  • It's unreachable because for a given expansion of the template based on the template arguments the function will only ever pass the condition and return true or fail and return false. There is no case where it could go either way for the same type. It's essentially expanding to

    if (true) {
      return true;
    }
    return false; // Obviously will never happen
    

    I'd rewrite it to only have a single return statement.

    template <typename T, typename VariantType>
    inline bool MatchMonostate( VariantType& variant )
    {
        SUPPRESS_C4100( variant );
        bool retval = false;
        if constexpr ( std::is_same_v<T, std::monostate> )
        {
            variant = std::monostate();
            retval = true;
        }
        return retval;
    }
    

    Also, in the case where the condition is true variant is not unused. You may want to move that line that suppresses the warning (which basically turns into (void)variant) to an else statement.