Clarification for Rule 5-2-4

Moderators: david ward, misra cpp

Post Reply
Posts: 12
Joined: Wed Sep 06, 2017 4:25 pm
Company: Gimpel Software

Clarification for Rule 5-2-4

Post by abgs » Fri Jul 26, 2019 3:06 pm

1. Does the phrase "functional notation cast" in the rule correspond to what the C++ standard calls an "Explicit type conversion (functional notation)", or is there a distinction?

2. Does the phrase "explicit constructor call" in the rule mean "explicit-constructor call" (a call to a constructor declared with the 'explicit' specifier) or "explicit constructor-call" (an explicit call to a constructor, as opposed to an implicit conversion that uses a user-defined converting constructor)?

3. The rationale refers to dangers associated with casts that "do not invoke a converting constructor", however a "converting constructor" is a constructor that was *not* declared with the 'explicit' specifier. Was this intended to refer to casts that *do* invoke a converting constructor [as opposed to an explicit constructor]?

4. In the first compliant example with the expression 'A(10)' the declared constructor 'A(int32_t)' is declared with the explicit specifier. Would the 'A(10)' example be non-compliant if this constructor declaration lacked the 'explicit' specifier?

5. In later versions of the C++ standard, "Explicit type conversion (functional notation)" includes a type name followed by a braced list, e.g. 'X{5}', which may be used for aggregate initialization for 'struct X { int32_t a; };'. Is it within the original spirit of the rule to include this sytnax?

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

Re: Clarification for Rule 5-2-4

Post by misra cpp » Wed Oct 16, 2019 12:01 pm

1) "functional notation cast" is what the C++ standard calls an "Explicit type conversion (functional notation)"
2) the presence or absence of the keyword “explicit” is not relevant
3) the intent is to allow function notation casts only if the destination type is a class type
4) A(10) would be compliant for this rule, but the declaration of A’s constructor would violate the rule that requires the use of “explicit” (12-1-3)
5) Our next release will consider future versions of the C++ standard, as at present the use of X{5} would violate current rule 1-0-1, but it would appear compliant with the spirit of the rule

To summarise, functional notation cast means int(32) is non-compliant. However, C(32) where C is a class with a constructor with an integer parameters is compliant, this is what was meant by 'an explicit constructor call'
Posted by and on behalf of
the MISRA C++ Working Group

Post Reply

Return to “6.5 Expressions (C++)”