Tal - 2024-11-18

The following code should be valid, but cppcheck complains no matter of if with constexpr or a regular if:

template <int b>
int f(int a)
{
    if constexpr (b >= 0) {
        return a << b;
    } else {
        return a << -b;
    }
}

int g() {
    return f<1>(2)
}

Yields both in my personal code and in http://cppcheck.net/demo/:

[test.cpp:7]: (error) Shifting by a negative value is undefined behaviour

CPPCheck should take into account the branching over the right operand (here, b) of the << and >> operators.