Menu

Operator precedence

mcandre
2021-04-17
2021-04-17
  • mcandre

    mcandre - 2021-04-17

    cppcheck complains that a bitwise-and in the conditional part of my ternary is ambiguous (clarifyCalculation).

    https://github.com/mcandre/swiper/blob/master/src/swiper.cpp#L31

    However, C++ Reference says that the precedence order is unambiguous:

    https://en.cppreference.com/w/cpp/language/operator_precedence

    Also, I have just about every warning enabled for MSVC, gcc, and clang. None of these compilers have complained about the arithmetic being ambiguous. Are you sure this cppcheck linting rule is not a false report?

     

    Last edit: mcandre 2021-04-17
  • Daniel Marjamäki

    it is 100% clear in the standard.

    the question is if human programmers can misread the code.

    I guess (pair[1] & 64 ? 55 : 48) is pretty clear.. it would be strange if the intention would be that the condition was 64.. so I guess we can prevent the warning in this case (when last operand in condition is a integer literal).

     

    Last edit: Daniel Marjamäki 2021-04-17
  • Daniel Marjamäki

    It's a stylistic message. If you are sure that nobody will misunderstand such code ever in your team then you can safely suppress it.

    For information , we check debian source code continuously. And we get such warnings here and there. At least in Debian, I have the feeling that there are quite often real mistakes found by this warning.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.