<?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 - 6.4 Standard conversions (C++)]]></title>
		<link>https://forum.misra.org.uk/</link>
		<description><![CDATA[MISRA Discussion Forums - https://forum.misra.org.uk]]></description>
		<pubDate>Thu, 05 Nov 2026 15:14:01 +0000</pubDate>
		<generator>MyBB</generator>
		<item>
			<title><![CDATA[Rule 4-5-3 about using relational operators to determine uint8_t]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1639</link>
			<pubDate>Thu, 08 Dec 2022 08:00:46 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5868">zhaohui</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1639</guid>
			<description><![CDATA[<blockquote class="mycode_quote"><cite>Quote:</cite><span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Monaco, Consolas, Courier, monospace;" class="mycode_font">Exceptionally, the following operators may be used if the associated restriction is observed:</span></span></span><br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Monaco, Consolas, Courier, monospace;" class="mycode_font">• The binary + operator may be used to add an integral value in the range 0 to 9 to ‘0’;</span></span></span><br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Monaco, Consolas, Courier, monospace;" class="mycode_font">• The binary – operator may be used to subtract character '0'</span></span></span><span style="font-size: x-small;" class="mycode_size"><span style="font-family: Monaco, Consolas, Courier, monospace;" class="mycode_font">.</span></span><br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Monaco, Consolas, Courier, monospace;" class="mycode_font">• The relational operators &lt;, &lt;=, &gt;, &gt;= may be used to determine if a character (or wide</span></span></span><br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Monaco, Consolas, Courier, monospace;" class="mycode_font">character) represents a digit.</span></span></span></blockquote>
According to exceptions and cases of Rule 4-5-3, it seems that Exp1 apples to <span style="font-style: italic;" class="mycode_i">uint8_t </span>and Exp2, Exp3 apply to character (wide character). And the rule title mainly cares about plain <span style="font-style: italic;" class="mycode_i">char </span>and <span style="font-style: italic;" class="mycode_i">wchar_t</span>, so, <br />
1. What about using uint8_t as operands of relational operators?<br />
2. Which kind of cases does uint8_t violate this rule? Does this rule only check binary operator "+" only for uint8_t?<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>void f(void)<br />
{<br />
    char ch = 't';<br />
    if (( ch &gt;= '0') &amp;&amp; ( ch &lt;= '9')) // Compliant by exception<br />
    {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v = ch – '0';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Compliant by exception<br />
    }<br />
<br />
    unsigned char uc;<br />
    if (( uc &gt;= '0') &amp;&amp; ( uc &lt;= '9'))  // compliant or non-compliant?<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
    }<br />
}</code></div></div>]]></description>
			<content:encoded><![CDATA[<blockquote class="mycode_quote"><cite>Quote:</cite><span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Monaco, Consolas, Courier, monospace;" class="mycode_font">Exceptionally, the following operators may be used if the associated restriction is observed:</span></span></span><br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Monaco, Consolas, Courier, monospace;" class="mycode_font">• The binary + operator may be used to add an integral value in the range 0 to 9 to ‘0’;</span></span></span><br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Monaco, Consolas, Courier, monospace;" class="mycode_font">• The binary – operator may be used to subtract character '0'</span></span></span><span style="font-size: x-small;" class="mycode_size"><span style="font-family: Monaco, Consolas, Courier, monospace;" class="mycode_font">.</span></span><br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Monaco, Consolas, Courier, monospace;" class="mycode_font">• The relational operators &lt;, &lt;=, &gt;, &gt;= may be used to determine if a character (or wide</span></span></span><br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Monaco, Consolas, Courier, monospace;" class="mycode_font">character) represents a digit.</span></span></span></blockquote>
According to exceptions and cases of Rule 4-5-3, it seems that Exp1 apples to <span style="font-style: italic;" class="mycode_i">uint8_t </span>and Exp2, Exp3 apply to character (wide character). And the rule title mainly cares about plain <span style="font-style: italic;" class="mycode_i">char </span>and <span style="font-style: italic;" class="mycode_i">wchar_t</span>, so, <br />
1. What about using uint8_t as operands of relational operators?<br />
2. Which kind of cases does uint8_t violate this rule? Does this rule only check binary operator "+" only for uint8_t?<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>void f(void)<br />
{<br />
    char ch = 't';<br />
    if (( ch &gt;= '0') &amp;&amp; ( ch &lt;= '9')) // Compliant by exception<br />
    {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v = ch – '0';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Compliant by exception<br />
    }<br />
<br />
    unsigned char uc;<br />
    if (( uc &gt;= '0') &amp;&amp; ( uc &lt;= '9'))  // compliant or non-compliant?<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
    }<br />
}</code></div></div>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[R4-5-1 Alternative tokens]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1579</link>
			<pubDate>Thu, 23 Sep 2021 10:26:27 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5700">ELovisari</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1579</guid>
			<description><![CDATA[Hello,<br />
<br />
I have a request for clarification for rule 4-5-1:<br />
<br />
<blockquote class="mycode_quote"><cite>Quote:</cite><span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Tahoma, Verdana, Arial, sans-serif;" class="mycode_font">Rule 4–5–1</span></span></span><br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Tahoma, Verdana, Arial, sans-serif;" class="mycode_font">(Required)</span></span></span><br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Tahoma, Verdana, Arial, sans-serif;" class="mycode_font">Expressions with type bool shall not be used as operands to built-in operators other than the </span></span></span><span style="font-size: small;" class="mycode_size">assignment operator = , the logical operators &amp;&amp; , || , ! , the equality operators == and !=, the unary &amp; operator, </span><span style="font-size: small;" class="mycode_size">and the conditional operator.</span></blockquote>
<br />
<br />
<br />
Rule 4-5-1 and its Rationale do not seem to explicitly forbid the alternative operator representations <span style="font-weight: bold;" class="mycode_b">and</span>, <span style="font-weight: bold;" class="mycode_b">or</span> and <span style="font-weight: bold;" class="mycode_b">not</span>, which, if I understand correctly, are equivalent to the allowed &amp;&amp;, || and !. <span style="color: #333333;" class="mycode_color"><span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Tahoma, Verdana, Arial, sans-serif;" class="mycode_font"><span style="font-size: small;" class="mycode_size"><span style="font-size: small;" class="mycode_size"><span style="font-family: Tahoma, Verdana, Arial, sans-serif;" class="mycode_font">Rule 2-5-1 explicitly forbids digraphs, but restricts the notion of digraph to the six cases given in its Rationale.</span></span></span></span></span></span></span><br />
<br />
Can I therefore interpret the rule as implicitly allowing <span style="font-weight: bold;" class="mycode_b">and</span>, <span style="font-weight: bold;" class="mycode_b">or</span> and <span style="font-weight: bold;" class="mycode_b">not</span> (and then maybe also <span style="font-weight: bold;" class="mycode_b">not_eq</span>) tokens?]]></description>
			<content:encoded><![CDATA[Hello,<br />
<br />
I have a request for clarification for rule 4-5-1:<br />
<br />
<blockquote class="mycode_quote"><cite>Quote:</cite><span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Tahoma, Verdana, Arial, sans-serif;" class="mycode_font">Rule 4–5–1</span></span></span><br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Tahoma, Verdana, Arial, sans-serif;" class="mycode_font">(Required)</span></span></span><br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Tahoma, Verdana, Arial, sans-serif;" class="mycode_font">Expressions with type bool shall not be used as operands to built-in operators other than the </span></span></span><span style="font-size: small;" class="mycode_size">assignment operator = , the logical operators &amp;&amp; , || , ! , the equality operators == and !=, the unary &amp; operator, </span><span style="font-size: small;" class="mycode_size">and the conditional operator.</span></blockquote>
<br />
<br />
<br />
Rule 4-5-1 and its Rationale do not seem to explicitly forbid the alternative operator representations <span style="font-weight: bold;" class="mycode_b">and</span>, <span style="font-weight: bold;" class="mycode_b">or</span> and <span style="font-weight: bold;" class="mycode_b">not</span>, which, if I understand correctly, are equivalent to the allowed &amp;&amp;, || and !. <span style="color: #333333;" class="mycode_color"><span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Tahoma, Verdana, Arial, sans-serif;" class="mycode_font"><span style="font-size: small;" class="mycode_size"><span style="font-size: small;" class="mycode_size"><span style="font-family: Tahoma, Verdana, Arial, sans-serif;" class="mycode_font">Rule 2-5-1 explicitly forbids digraphs, but restricts the notion of digraph to the six cases given in its Rationale.</span></span></span></span></span></span></span><br />
<br />
Can I therefore interpret the rule as implicitly allowing <span style="font-weight: bold;" class="mycode_b">and</span>, <span style="font-weight: bold;" class="mycode_b">or</span> and <span style="font-weight: bold;" class="mycode_b">not</span> (and then maybe also <span style="font-weight: bold;" class="mycode_b">not_eq</span>) tokens?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 4-5-1 prohibits 'b1 |= b2;' ?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1277</link>
			<pubDate>Wed, 28 Sep 2016 14:00:17 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=3331">grunwald</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1277</guid>
			<description><![CDATA[Neither `|` nor `|=` is listed as allowed operator.<br />
Does this mean the following code a violation of Rule 4-5-1?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>bool b1 = false;<br />
<br />
void f(int x)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;b1 |= (x == 0);<br />
}</code></div></div>]]></description>
			<content:encoded><![CDATA[Neither `|` nor `|=` is listed as allowed operator.<br />
Does this mean the following code a violation of Rule 4-5-1?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>bool b1 = false;<br />
<br />
void f(int x)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;b1 |= (x == 0);<br />
}</code></div></div>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 4-5-1 prohibits sizeof(bool_type_variable)?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1257</link>
			<pubDate>Mon, 13 Jun 2016 04:56:23 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4420">udi</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1257</guid>
			<description><![CDATA[According to Rule 4-5-1 sizeof(bool_type_variable) is not allowed, although it is very meaningful in serialization.<br />
Is this intentional?<br />
If so then why?]]></description>
			<content:encoded><![CDATA[According to Rule 4-5-1 sizeof(bool_type_variable) is not allowed, although it is very meaningful in serialization.<br />
Is this intentional?<br />
If so then why?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Resolve Rule 5-0-6: cast to 7 bits struct field]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1097</link>
			<pubDate>Tue, 12 Aug 2014 15:44:54 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=2557">pvarela</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1097</guid>
			<description><![CDATA[How can resolve next Notes??<br />
<br />
<span style="font-weight: bold;" class="mycode_b">Note 1960: Violates MISRA C++ 2008 Required Rule 5-0-6, Implicit conversion of integer to smaller type<br />
Note 1960: Violates MISRA C++ 2008 Required Rule 5-0-3, Implicit conversion of integer cvalue expression</span><br />
<br />
In this assignament:<br />
<span style="text-decoration: underline;" class="mycode_u">CTU.EVTCFGR[fTriggerChannel].B.CHANNEL_VALUE = static_cast (fAdcChannel &amp; 0xFFU);</span><br />
<br />
Where CHANNEL_VALUE is:<br />
  <span style="font-style: italic;" class="mycode_i">  union { /* Event Config 0..63 (Base+0x0030-0x012C) */<br />
        vuint32_t R;<br />
        struct {<br />
            vuint32_t :16;<br />
            vuint32_t TM:1;<br />
            vuint32_t CLR_FLAG:1;<br />
            vuint32_t :5;<br />
            vuint32_t ADC_SEL:1;<br />
            vuint32_t :1;<br />
            vuint32_t CHANNEL_VALUE:7;<br />
        } B;<br />
    } EVTCFGR[64];</span><br />
<br />
Thanks in advance]]></description>
			<content:encoded><![CDATA[How can resolve next Notes??<br />
<br />
<span style="font-weight: bold;" class="mycode_b">Note 1960: Violates MISRA C++ 2008 Required Rule 5-0-6, Implicit conversion of integer to smaller type<br />
Note 1960: Violates MISRA C++ 2008 Required Rule 5-0-3, Implicit conversion of integer cvalue expression</span><br />
<br />
In this assignament:<br />
<span style="text-decoration: underline;" class="mycode_u">CTU.EVTCFGR[fTriggerChannel].B.CHANNEL_VALUE = static_cast (fAdcChannel &amp; 0xFFU);</span><br />
<br />
Where CHANNEL_VALUE is:<br />
  <span style="font-style: italic;" class="mycode_i">  union { /* Event Config 0..63 (Base+0x0030-0x012C) */<br />
        vuint32_t R;<br />
        struct {<br />
            vuint32_t :16;<br />
            vuint32_t TM:1;<br />
            vuint32_t CLR_FLAG:1;<br />
            vuint32_t :5;<br />
            vuint32_t ADC_SEL:1;<br />
            vuint32_t :1;<br />
            vuint32_t CHANNEL_VALUE:7;<br />
        } B;<br />
    } EVTCFGR[64];</span><br />
<br />
Thanks in advance]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 4-10-2 NULL is not a c++ standard]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1026</link>
			<pubDate>Fri, 07 Feb 2014 07:34:33 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=2439">minhyuk</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1026</guid>
			<description><![CDATA[In C++ Standard, There are not exist NULL(its C standard).<br />
So, Programmer should to know about this and Use constant 0 instead of NULL macro.]]></description>
			<content:encoded><![CDATA[In C++ Standard, There are not exist NULL(its C standard).<br />
So, Programmer should to know about this and Use constant 0 instead of NULL macro.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Casting pointers & Rule 5-2-7]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=885</link>
			<pubDate>Tue, 28 Feb 2012 13:38:24 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=1969">rfrutos</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=885</guid>
			<description><![CDATA[The 5-2-7 rule says:<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>An object with pointer type sall not be converted to an unrelated pointer type, either directly or indirectly</blockquote>
<br />
and then shows some examples of casting between classes and structs, my question is if that is extensible to castings to primary types.<br />
What I would like to do is something like:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>typedef struct SC_MsgFrameToRbc<br />
{<br />
&nbsp;&nbsp;uint32_t idIxl;<br />
&nbsp;&nbsp;uint32_t size;<br />
&nbsp;&nbsp;uint8_t frame[MAX_SIZE_FRAME];<br />
} SC_MsgFrameToRbc;<br />
<br />
SC_MsgFrameToRbc *msgReceived = NULL;<br />
SC_MsgFrameToRbc msgSended = NULL;<br />
char *buffer = NULL;<br />
<br />
<br />
msgSended.idIxl = 1;<br />
msgSended.size = 2<br />
...<br />
<br />
// Simulates sending message<br />
memcpy(buffer, reinterpret_cast(&amp;msgSended), sizeof(SC_MsgFrameToRbc)); // Compliant ??<br />
<br />
// Message reception<br />
msgReceived = reinterpret_cast(buffer); // Compliant ??</code></div></div><br />
where the memcpy simulates other system-dependent code that implements message sending, would the two castings be compliant?]]></description>
			<content:encoded><![CDATA[The 5-2-7 rule says:<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>An object with pointer type sall not be converted to an unrelated pointer type, either directly or indirectly</blockquote>
<br />
and then shows some examples of casting between classes and structs, my question is if that is extensible to castings to primary types.<br />
What I would like to do is something like:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>typedef struct SC_MsgFrameToRbc<br />
{<br />
&nbsp;&nbsp;uint32_t idIxl;<br />
&nbsp;&nbsp;uint32_t size;<br />
&nbsp;&nbsp;uint8_t frame[MAX_SIZE_FRAME];<br />
} SC_MsgFrameToRbc;<br />
<br />
SC_MsgFrameToRbc *msgReceived = NULL;<br />
SC_MsgFrameToRbc msgSended = NULL;<br />
char *buffer = NULL;<br />
<br />
<br />
msgSended.idIxl = 1;<br />
msgSended.size = 2<br />
...<br />
<br />
// Simulates sending message<br />
memcpy(buffer, reinterpret_cast(&amp;msgSended), sizeof(SC_MsgFrameToRbc)); // Compliant ??<br />
<br />
// Message reception<br />
msgReceived = reinterpret_cast(buffer); // Compliant ??</code></div></div><br />
where the memcpy simulates other system-dependent code that implements message sending, would the two castings be compliant?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Relational operators and plain chars and wchar_t]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=725</link>
			<pubDate>Fri, 26 Mar 2010 15:41:01 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=64">gs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=725</guid>
			<description><![CDATA[The documentation for MISRA C++ rule #4-5-3 includes the following:<blockquote class="mycode_quote"><cite>Quote:</cite>The relational operators = may be used to determine if a character (or wide character) represents a digit.</blockquote>
The document then includes several examples which demonstrate compliance (or lack thereof) for the '&gt;=' and ']]></description>
			<content:encoded><![CDATA[The documentation for MISRA C++ rule #4-5-3 includes the following:<blockquote class="mycode_quote"><cite>Quote:</cite>The relational operators = may be used to determine if a character (or wide character) represents a digit.</blockquote>
The document then includes several examples which demonstrate compliance (or lack thereof) for the '&gt;=' and ']]></content:encoded>
		</item>
	</channel>
</rss>