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
#4
@misracpp does my above clarification change your view on the issue?

I also note that rule 7.0.5 does have an exception for constant non-negative signed values that are converted to unsigned:

> A compile-time constant with signed integral type that has a non-negative value may be converted to an unsigned type through the usual arithmetic conversions.

Would it be reasonable to apply the same exception for 7.0.4?
Reply
#5
Its an interesting use case, but we suggest you should apply a wrapper around the third-party code, to convert the flags to unsigned constants or enums with unsigned underlying type
Posted by and on behalf of
the MISRA C++ Working Group
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)