<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title><![CDATA[MISRA Discussion Forums - 4.7 Standard conversions]]></title>
		<link>https://forum.misra.org.uk/</link>
		<description><![CDATA[MISRA Discussion Forums - https://forum.misra.org.uk]]></description>
		<pubDate>Thu, 12 Nov 2026 13:37:46 +0000</pubDate>
		<generator>MyBB</generator>
		<item>
			<title><![CDATA[Rule 7.0.2: Why the requirement for 'explicit' conversion operator?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1758</link>
			<pubDate>Thu, 27 Nov 2025 09:04:13 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5614">cgpzs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1758</guid>
			<description><![CDATA[Rule 7.0.2 has exceptions for "a class type having an explicit operator bool."<br />
<br />
Question: why does the conversion operator have to be 'explicit' for the exception to apply? Even if the conversion operator is implicit, clearly the class was designed to be convertible to bool. This requirement seems therefore excessive.<br />
<br />
I'm thinking for example about std::bitset::operator[] which returns a proxy object convertible to bool. However since the conversion operator is not explicit (as specified in the C++ standard), then even performing an explicit cast to bool from the proxy object violates Rule 7.0.2. <br />
<br />
Note: I am aware that the 'const' version of std::bitset::operator[] should return a 'bool' directly, but some implementations return the proxy object also here, so the conversion is still necessary.]]></description>
			<content:encoded><![CDATA[Rule 7.0.2 has exceptions for "a class type having an explicit operator bool."<br />
<br />
Question: why does the conversion operator have to be 'explicit' for the exception to apply? Even if the conversion operator is implicit, clearly the class was designed to be convertible to bool. This requirement seems therefore excessive.<br />
<br />
I'm thinking for example about std::bitset::operator[] which returns a proxy object convertible to bool. However since the conversion operator is not explicit (as specified in the C++ standard), then even performing an explicit cast to bool from the proxy object violates Rule 7.0.2. <br />
<br />
Note: I am aware that the 'const' version of std::bitset::operator[] should return a 'bool' directly, but some implementations return the proxy object also here, so the conversion is still necessary.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 7.0.5, example non-compliant with 7.0.6?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1735</link>
			<pubDate>Thu, 17 Apr 2025 12:10:54 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5614">cgpzs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1735</guid>
			<description><![CDATA[Rule 7.0.5 presents this example:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>u8a += static_cast&lt; uint32_t &gt;( u8b ) // Compliant - u8a -&gt; unsigned int</code></div></div><br />
Yes, compliant with 7.0.5. But this code then violates 7.0.6, since there is an implicit narrowing conversion from uint32_t to uint8_t in the assignment, right?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>u8a = (uint32_t)(u8a) + (uint32_t)(u8b);</code></div></div><br />
So, how are we supposed to write this type of code? Like this?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>u8a = static_cast&lt;std::uint8_t&gt;(static_cast&lt;std::uint32_t&gt;(u8a) + static_cast&lt;std::uint32_t&gt;(u8b));</code></div></div><br />
Does that really make the code safer?]]></description>
			<content:encoded><![CDATA[Rule 7.0.5 presents this example:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>u8a += static_cast&lt; uint32_t &gt;( u8b ) // Compliant - u8a -&gt; unsigned int</code></div></div><br />
Yes, compliant with 7.0.5. But this code then violates 7.0.6, since there is an implicit narrowing conversion from uint32_t to uint8_t in the assignment, right?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>u8a = (uint32_t)(u8a) + (uint32_t)(u8b);</code></div></div><br />
So, how are we supposed to write this type of code? Like this?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>u8a = static_cast&lt;std::uint8_t&gt;(static_cast&lt;std::uint32_t&gt;(u8a) + static_cast&lt;std::uint32_t&gt;(u8b));</code></div></div><br />
Does that really make the code safer?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 7.0.6 - why the requirement of "id-expression"?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1720</link>
			<pubDate>Thu, 09 Jan 2025 08:43:25 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5614">cgpzs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1720</guid>
			<description><![CDATA[Rule 7.0.6 provides the following amplification for assigning numerical types:<br />
<br />
2. The source and target shall have types of the same type category, signedness, the source size<br />
shall be smaller than the target size, <span style="font-weight: bold;" class="mycode_b">and the source shall be an id-expression</span>;<br />
<br />
Question: why must the source be an "id-expression"?<br />
<br />
Consider this example:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct Foo<br />
{<br />
&nbsp;&nbsp;float bar;<br />
};<br />
<br />
float a;<br />
Foo f;<br />
<br />
double d1 = a;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Compliant, since it's an id-expression<br />
double d2 = f.bar;&nbsp;&nbsp;// Non-compliant, since it's NOT an id-expression (it's a member-expression)</code></div></div><br />
This is inconsistent. The fact that we are using a member-expression instead of an id-expression does not have any impact on how the conversion is made, it's equally safe. <br />
<br />
Would it make sense to remove the "id-expression" part of the rule? Otherwise, can you elaborate on why is it needed, what problems does it guard against?<br />
<br />
Thanks!]]></description>
			<content:encoded><![CDATA[Rule 7.0.6 provides the following amplification for assigning numerical types:<br />
<br />
2. The source and target shall have types of the same type category, signedness, the source size<br />
shall be smaller than the target size, <span style="font-weight: bold;" class="mycode_b">and the source shall be an id-expression</span>;<br />
<br />
Question: why must the source be an "id-expression"?<br />
<br />
Consider this example:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct Foo<br />
{<br />
&nbsp;&nbsp;float bar;<br />
};<br />
<br />
float a;<br />
Foo f;<br />
<br />
double d1 = a;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Compliant, since it's an id-expression<br />
double d2 = f.bar;&nbsp;&nbsp;// Non-compliant, since it's NOT an id-expression (it's a member-expression)</code></div></div><br />
This is inconsistent. The fact that we are using a member-expression instead of an id-expression does not have any impact on how the conversion is made, it's equally safe. <br />
<br />
Would it make sense to remove the "id-expression" part of the rule? Otherwise, can you elaborate on why is it needed, what problems does it guard against?<br />
<br />
Thanks!]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[rule 7.0.5: clarification regarding scope / expressions vs full expressions]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1719</link>
			<pubDate>Mon, 06 Jan 2025 11:56:20 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=6552">sks</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1719</guid>
			<description><![CDATA[Hi,<br />
<br />
as I understand it right now, rule 7.0.5 is very strict and often prevents unsigned integer types smaller than int from being used in a MISRA-compliant way. For example, take<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>const uint8_t cu8a = 0;<br />
const uint16_t cu16a = 0;<br />
static_assert(cu8a == cu16a);</code></div></div><br />
Here, integral promotions happen on both the lhs and rhs of the == operator. Both are implicitly converted to int, thus changing from unsigned to signed, causing a violation to MisraC++ 2023 Rule 7.0.5.<br />
<br />
But does Rule 7.0.5 even apply in this case? The rule is supposed to apply to "all expressions (including sub-expressions) of _numeric_ type". "cu8a == cu16a" is of type bool, so the rule does not apply to the full expression. However, the expressions on the LHS and RHS individually are of a numerical type and consist of the implicit "(int)cu8a" / "(int)cu16a.<br />
<br />
The rule also states "For the usual arithmetic conversions, only the final type of an operand is considered." Which could suggest a restriction for operators that return integral types (otherwise the expression itself would not be of integral type and not considered at all).<br />
<br />
The same case could be made with an implicit cast to bool, eg taking the sum or difference of cu8a and cu16a.<br />
<br />
How exactly is the rule supposed to be scoped and what expressions / operands does it consider?]]></description>
			<content:encoded><![CDATA[Hi,<br />
<br />
as I understand it right now, rule 7.0.5 is very strict and often prevents unsigned integer types smaller than int from being used in a MISRA-compliant way. For example, take<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>const uint8_t cu8a = 0;<br />
const uint16_t cu16a = 0;<br />
static_assert(cu8a == cu16a);</code></div></div><br />
Here, integral promotions happen on both the lhs and rhs of the == operator. Both are implicitly converted to int, thus changing from unsigned to signed, causing a violation to MisraC++ 2023 Rule 7.0.5.<br />
<br />
But does Rule 7.0.5 even apply in this case? The rule is supposed to apply to "all expressions (including sub-expressions) of _numeric_ type". "cu8a == cu16a" is of type bool, so the rule does not apply to the full expression. However, the expressions on the LHS and RHS individually are of a numerical type and consist of the implicit "(int)cu8a" / "(int)cu16a.<br />
<br />
The rule also states "For the usual arithmetic conversions, only the final type of an operand is considered." Which could suggest a restriction for operators that return integral types (otherwise the expression itself would not be of integral type and not considered at all).<br />
<br />
The same case could be made with an implicit cast to bool, eg taking the sum or difference of cu8a and cu16a.<br />
<br />
How exactly is the rule supposed to be scoped and what expressions / operands does it consider?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 7.0.2: Unclear/questionable rationale]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1718</link>
			<pubDate>Fri, 06 Dec 2024 09:29:45 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5614">cgpzs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1718</guid>
			<description><![CDATA[Hi,<br />
<br />
Rule 7.0.2 bans any type of conversion to bool (implicit or explicit) anywhere in the code (with some exceptions). The rationale says:<br />
<br />
"However, this interpretation may not be appropriate for APIs, such as POSIX, that do not use Boolean return values."<br />
<br />
This sounds strange, why should modern C++17 code (that may not use C-style POSIX functions at all) be limited by this?<br />
<br />
Could you show an example code that highlights how this is a problem and leads to unexpected behavior? For example, how is this code unsafe/can lead to unexpected results?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>bool b1 = static_cast&lt; bool &gt;( 4 );&nbsp;&nbsp;// Non-compliant</code></div></div><br />
Conversion from fundamental types to bool is well-defined behavior as per [conv.bool]:<br />
<br />
"A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true."<br />
<br />
Thanks!]]></description>
			<content:encoded><![CDATA[Hi,<br />
<br />
Rule 7.0.2 bans any type of conversion to bool (implicit or explicit) anywhere in the code (with some exceptions). The rationale says:<br />
<br />
"However, this interpretation may not be appropriate for APIs, such as POSIX, that do not use Boolean return values."<br />
<br />
This sounds strange, why should modern C++17 code (that may not use C-style POSIX functions at all) be limited by this?<br />
<br />
Could you show an example code that highlights how this is a problem and leads to unexpected behavior? For example, how is this code unsafe/can lead to unexpected results?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>bool b1 = static_cast&lt; bool &gt;( 4 );&nbsp;&nbsp;// Non-compliant</code></div></div><br />
Conversion from fundamental types to bool is well-defined behavior as per [conv.bool]:<br />
<br />
"A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true."<br />
<br />
Thanks!]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 7.0.4 - exception for binary bitwise operators and constant operands?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1717</link>
			<pubDate>Thu, 05 Dec 2024 13:06:01 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5614">cgpzs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1717</guid>
			<description><![CDATA[Hi,<br />
<br />
My interpretation of Rule 7.0.4 is that it does not allow this type of code:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>my_function(FLAG_A | FLAG_B | FLAG_C);</code></div></div><br />
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.<br />
<br />
Would it make sense to add an exception for signed constants?<br />
<br />
Thanks!]]></description>
			<content:encoded><![CDATA[Hi,<br />
<br />
My interpretation of Rule 7.0.4 is that it does not allow this type of code:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>my_function(FLAG_A | FLAG_B | FLAG_C);</code></div></div><br />
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.<br />
<br />
Would it make sense to add an exception for signed constants?<br />
<br />
Thanks!]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 7.0.2: operator const char *()]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1710</link>
			<pubDate>Fri, 11 Oct 2024 12:33:28 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=6503">karos</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1710</guid>
			<description><![CDATA[Hello,<br />
<br />
Rule 7.0.2 has an exception for converting pointer to bool, and requires an explicit operator bool() for classes. But what if a class, let's say a wrapper class for strings, provides an operator const char *? Consider the following example:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>class C<br />
{<br />
public:<br />
    operator const char *() const;<br />
};<br />
<br />
void f(C c)<br />
{<br />
    if (c); // C is converted twice: C -&gt; const char * -&gt; bool<br />
}</code></div></div><br />
Shall the commented line be considered compliant with this rule (because each of the conversion steps by itself is compliant), or not (because as a whole it is the conversion of a class to bool without using an explicit operator bool)?]]></description>
			<content:encoded><![CDATA[Hello,<br />
<br />
Rule 7.0.2 has an exception for converting pointer to bool, and requires an explicit operator bool() for classes. But what if a class, let's say a wrapper class for strings, provides an operator const char *? Consider the following example:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>class C<br />
{<br />
public:<br />
    operator const char *() const;<br />
};<br />
<br />
void f(C c)<br />
{<br />
    if (c); // C is converted twice: C -&gt; const char * -&gt; bool<br />
}</code></div></div><br />
Shall the commented line be considered compliant with this rule (because each of the conversion steps by itself is compliant), or not (because as a whole it is the conversion of a class to bool without using an explicit operator bool)?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 7.0.4 - example not compliant with 7.0.5?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1703</link>
			<pubDate>Thu, 12 Sep 2024 12:56:09 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5614">cgpzs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1703</guid>
			<description><![CDATA[Hi,<br />
<br />
Rule 7.0.4 provides this example as compliant, and does not violate other rules  (like other examples)<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>static_cast&lt; uint16_t &gt;( u8 + u16 ) &lt;&lt; 2;&nbsp;&nbsp;// Compliant</code></div></div><br />
Why doesn't this example violate 7.0.5? Bit shift applied on uint16_t will cause integral promotion from uint16_t to int, thus violating 7.0.5, or?<br />
<br />
Thanks!]]></description>
			<content:encoded><![CDATA[Hi,<br />
<br />
Rule 7.0.4 provides this example as compliant, and does not violate other rules  (like other examples)<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>static_cast&lt; uint16_t &gt;( u8 + u16 ) &lt;&lt; 2;&nbsp;&nbsp;// Compliant</code></div></div><br />
Why doesn't this example violate 7.0.5? Bit shift applied on uint16_t will cause integral promotion from uint16_t to int, thus violating 7.0.5, or?<br />
<br />
Thanks!]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule R7.0.1 converting constructor question]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1700</link>
			<pubDate>Thu, 22 Aug 2024 19:58:16 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=1246">gdavis</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1700</guid>
			<description><![CDATA[Hello,<br />
<br />
Rule 7.0.1 allows for an explicit cast from bool to T when T has a converting constructor with a parameter of type bool. The example for the rule shows:<br />
<br />
<span style="font-family: Courier New;" class="mycode_font">struct A</span><br />
<span style="font-family: Courier New;" class="mycode_font">{</span><br />
<span style="font-family: Courier New;" class="mycode_font">  explicit A ( bool );</span><br />
<span style="font-family: Courier New;" class="mycode_font">};</span><br />
<br />
<span style="font-family: Courier New;" class="mycode_font">A anA { true };       // Compliant - constructor</span><br />
<br />
Yet, C++ requires a converting constructor to be non-explicit.<br />
<br />
I'm not sure what the intent is, but perhaps the rule should allow for an explicit cast from B to T when T has an explicit constructor that takes a single parameter, a parameter of (possibly const-qualified) type bool.<br />
<br />
Please advise. Thank you.]]></description>
			<content:encoded><![CDATA[Hello,<br />
<br />
Rule 7.0.1 allows for an explicit cast from bool to T when T has a converting constructor with a parameter of type bool. The example for the rule shows:<br />
<br />
<span style="font-family: Courier New;" class="mycode_font">struct A</span><br />
<span style="font-family: Courier New;" class="mycode_font">{</span><br />
<span style="font-family: Courier New;" class="mycode_font">  explicit A ( bool );</span><br />
<span style="font-family: Courier New;" class="mycode_font">};</span><br />
<br />
<span style="font-family: Courier New;" class="mycode_font">A anA { true };       // Compliant - constructor</span><br />
<br />
Yet, C++ requires a converting constructor to be non-explicit.<br />
<br />
I'm not sure what the intent is, but perhaps the rule should allow for an explicit cast from B to T when T has an explicit constructor that takes a single parameter, a parameter of (possibly const-qualified) type bool.<br />
<br />
Please advise. Thank you.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 7.0.5 Example potential typo]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1677</link>
			<pubDate>Thu, 21 Mar 2024 13:57:02 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=6367">danix800</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1677</guid>
			<description><![CDATA[```c<br />
constexpr int32_t fn( int32_t i )<br />
{<br />
  return i * i;<br />
}<br />
<br />
u8 + fn( 10 )   // Compliant by exception #1<br />
```<br />
<br />
Is u8 a typo? Reasonable one might be 'u32 + fn(10)'?<br />
<hr class="mycode_hr" />
u8 &lt;&lt; 2  is non-compliant, so should u8 + f(10), this should be a typo.]]></description>
			<content:encoded><![CDATA[```c<br />
constexpr int32_t fn( int32_t i )<br />
{<br />
  return i * i;<br />
}<br />
<br />
u8 + fn( 10 )   // Compliant by exception #1<br />
```<br />
<br />
Is u8 a typo? Reasonable one might be 'u32 + fn(10)'?<br />
<hr class="mycode_hr" />
u8 &lt;&lt; 2  is non-compliant, so should u8 + f(10), this should be a typo.]]></content:encoded>
		</item>
	</channel>
</rss>