Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 7.0.4 - exception for binary bitwise operators and constant operands?
#1
Hi,

My interpretation of Rule 7.0.4 is that it does not allow this type of code:

Code:
my_function(FLAG_A | FLAG_B | FLAG_C);

This is quite frequent when interfacing with third-party as well as POSIX C APIs. Casting each argument to unsigned type would go in detriment of readability.

Would it make sense to add an exception for signed constants?

Thanks!
Reply
#2
We don't wish to encourage the use of signed values in bitwise expressions, even if constant - so are not planning an exception.

We are assuming that FLAG_A etc. are C-style enums, so the code also violates 10.2.3 "The numeric value of an unscoped enumeration with no fixed underlying type shall not be used"

You need to deviate each such expression, or better define your own bitwise operator for this enum type (which will still need a deviation for 10.2.3 - but only in one place)
Posted by and on behalf of
the MISRA C++ Working Group
Reply
#3
Sorry, I should have been more explicit. FLAG_A, etc are C-style positive signed integer #defines, like:

#define FLAG_A 0x0001
#define FLAG_B 0x0002
#define FLAG_C 0x0004

Thus we cannot unfortunately define a bitwise operator for them.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)