Rule 4-5-1 prohibits sizeof(bool_type_variable)?

Moderators: david ward, misra cpp

Post Reply
udi
Posts: 9
Joined: Wed May 11, 2016 8:24 am
Company: Elbit

Rule 4-5-1 prohibits sizeof(bool_type_variable)?

Post by udi » Mon Jun 13, 2016 5:56 am

According to Rule 4-5-1 sizeof(bool_type_variable) is not allowed, although it is very meaningful in serialization.
Is this intentional?
If so then why?

dg1980
Posts: 109
Joined: Wed Apr 27, 2016 2:33 pm
Company: Elektrobit Automotive GmbH

Re: Rule 4-5-1 prohibits sizeof(bool_type_variable)?

Post by dg1980 » Mon Jun 13, 2016 10:31 am

I think this is because sizeof(bool) is implementation-defined according to ISO C++03 chapter 5.3.3.
If you want to code a portable serialization function, you have to store it in an integer with a guaranteed fixed size across all platforms (e.g. using uint8_t from stdint.h or your own type header)

udi
Posts: 9
Joined: Wed May 11, 2016 8:24 am
Company: Elbit

Re: Rule 4-5-1 prohibits sizeof(bool_type_variable)?

Post by udi » Mon Jun 13, 2016 11:05 am

Thanks for the reply, but:
1. Still the use of cstdint types is only advisory. The same problem exists for all built-in types, so why bool should be different? The use of typedefs for bool, could be advisory as well (if that's the intention).
2. The rationale in this section, referes only to meaningless and therfore error-prone. The use of sizeof is not meaningless.
3. The portability of types can be verified with static_assert (or otherwise equivalent implementations of it).

By not addressing this issue directly in the standard, it becomes easier for me to ignore it as an advisory or overlooked.

As I do take meaures to avoid copmiler differences, where it might affect our S/W, my problem is that the rule for this in the static analysis tool we use, makes it harder for me to spot what I consider the "real" violations of this rule.

misra cpp
Posts: 150
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: Rule 4-5-1 prohibits sizeof(bool_type_variable)?

Post by misra cpp » Mon Oct 31, 2016 3:12 pm

You're right that the C++ standard does describe sizeof as an operator, and sizeof(bool) is a well defined operation so ought to be allowed

This will be addressed in a future edition of the rules
Posted by and on behalf of
the MISRA C++ Working Group

Post Reply

Return to “6.4 Standard conversions (C++)”