<?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.5 Expressions (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:13:56 +0000</pubDate>
		<generator>MyBB</generator>
		<item>
			<title><![CDATA[M5-0-4 switch over unscoped enum without fixed underlying type]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1785</link>
			<pubDate>Wed, 16 Sep 2026 11:08:53 +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=1785</guid>
			<description><![CDATA[Hi,<br />
<br />
Our static analyzer is flagging this code:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>// third_party.h<br />
typedef enum <br />
{<br />
  FOO_A,<br />
  FOO_B<br />
} Foo;<br />
<br />
// main.cpp<br />
#include &lt;third_party.h&gt;<br />
<br />
int get(Foo x)<br />
{<br />
  switch(x)            // Non-compliant with M5-0-4 ?<br />
  {<br />
    case FOO_A:        // Non-compliant with M5-0-4 ?<br />
      return 123;<br />
    case FOO_B:        // Non-compliant with M5-0-4 ?<br />
      return 321;<br />
  }<br />
  return -1;<br />
}</code></div></div><br />
The rationale from the supplier is:<br />
<ul class="mycode_list"><li>"Foo" is an unscoped enum without fixed underlying type.<br />
</li>
<li>Thus, its underlying type is implementation-defined.<br />
</li>
<li>On this implementation, the actual type is "unsigned int".<br />
</li>
<li>The line with "switch(x)" performs an integral promotion of "x" to "int", <span style="font-weight: bold;" class="mycode_b">thus changing the signedness</span> from "unsigned int" to "int".<br />
</li>
<li>Similarly, the "case FOO_*" lines also perform an integral promotion to "int".<br />
</li>
<li>This change in signedness is a violation of M5-0-4.<br />
</li>
</ul>
<br />
We argue however that the code is perfectly safe and idiomatic, the Standard guarantees that the conversion is made to an integer able to hold all the values of the enumerator, so it's impossible for data loss to occur.<br />
<br />
What is your opinion, is M5-0-4 truly applicable in this case?<br />
<br />
Thanks!]]></description>
			<content:encoded><![CDATA[Hi,<br />
<br />
Our static analyzer is flagging this code:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>// third_party.h<br />
typedef enum <br />
{<br />
  FOO_A,<br />
  FOO_B<br />
} Foo;<br />
<br />
// main.cpp<br />
#include &lt;third_party.h&gt;<br />
<br />
int get(Foo x)<br />
{<br />
  switch(x)            // Non-compliant with M5-0-4 ?<br />
  {<br />
    case FOO_A:        // Non-compliant with M5-0-4 ?<br />
      return 123;<br />
    case FOO_B:        // Non-compliant with M5-0-4 ?<br />
      return 321;<br />
  }<br />
  return -1;<br />
}</code></div></div><br />
The rationale from the supplier is:<br />
<ul class="mycode_list"><li>"Foo" is an unscoped enum without fixed underlying type.<br />
</li>
<li>Thus, its underlying type is implementation-defined.<br />
</li>
<li>On this implementation, the actual type is "unsigned int".<br />
</li>
<li>The line with "switch(x)" performs an integral promotion of "x" to "int", <span style="font-weight: bold;" class="mycode_b">thus changing the signedness</span> from "unsigned int" to "int".<br />
</li>
<li>Similarly, the "case FOO_*" lines also perform an integral promotion to "int".<br />
</li>
<li>This change in signedness is a violation of M5-0-4.<br />
</li>
</ul>
<br />
We argue however that the code is perfectly safe and idiomatic, the Standard guarantees that the conversion is made to an integer able to hold all the values of the enumerator, so it's impossible for data loss to occur.<br />
<br />
What is your opinion, is M5-0-4 truly applicable in this case?<br />
<br />
Thanks!]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[M5-0-20 clarification]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1640</link>
			<pubDate>Wed, 25 Jan 2023 16:37:10 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=6061">rt1980</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1640</guid>
			<description><![CDATA[Hi All,<br />
<br />
Rule 5-0-20 states that "Non-constant operands to a binary bitwise operator shall have the same underlying type." seems to make sense but my colleagues and I are bit unsure why the non-constant qualifier? <br />
<br />
Consider the following example. It's not clear to us why the first should be allowed if the second is not.<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>int foo(int i) {<br />
  const unsigned char mask = ~(0x10);<br />
  return i ^ mask; // compliant: mask is const<br />
}<br />
<br />
int foo(int i) {<br />
&nbsp;&nbsp;unsigned char mask = ~(0x10);<br />
&nbsp;&nbsp;return i ^ mask; // non-compliant: mask is not const and a different type than i<br />
}</code></div></div><br />
<br />
Thanks for your help!<br />
Rafe]]></description>
			<content:encoded><![CDATA[Hi All,<br />
<br />
Rule 5-0-20 states that "Non-constant operands to a binary bitwise operator shall have the same underlying type." seems to make sense but my colleagues and I are bit unsure why the non-constant qualifier? <br />
<br />
Consider the following example. It's not clear to us why the first should be allowed if the second is not.<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>int foo(int i) {<br />
  const unsigned char mask = ~(0x10);<br />
  return i ^ mask; // compliant: mask is const<br />
}<br />
<br />
int foo(int i) {<br />
&nbsp;&nbsp;unsigned char mask = ~(0x10);<br />
&nbsp;&nbsp;return i ^ mask; // non-compliant: mask is not const and a different type than i<br />
}</code></div></div><br />
<br />
Thanks for your help!<br />
Rafe]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Why example in Rule 5-0-13 is non-compliant]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1629</link>
			<pubDate>Wed, 27 Jul 2022 19:13:35 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5951">kafka</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1629</guid>
			<description><![CDATA[Could someone please explain why the "if ( u8 &amp;&amp; (bool_1 &lt;= bool_2 ) )" in Rule 5-0-13 is non-compliant.]]></description>
			<content:encoded><![CDATA[Could someone please explain why the "if ( u8 &amp;&amp; (bool_1 &lt;= bool_2 ) )" in Rule 5-0-13 is non-compliant.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[5-2-10 with non-arithmetic operators]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1611</link>
			<pubDate>Tue, 05 Apr 2022 12:34:42 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5872">Tobias Loose</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1611</guid>
			<description><![CDATA[Hi!<br />
<br />
The rule states <br />
<br />
<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>The increment (++) and decrement (--) operators should not be mixed with other operators in an expression.</blockquote>
<br />
<br />
The code checker we employ also flags the usage of these operators in expressions that contain member-access operators, subscript operators, and the like.<br />
<br />
The rationale and example appear to focus on other <span style="font-weight: bold;" class="mycode_b">arithmetic </span>operators, which makes sense.<br />
<br />
Is this an oversight in the rule formulation (missing "arithmetic"), or is the following intentionally non-compliant with 5-2-10?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>void foo()<br />
{<br />
    struct A { int b; };<br />
<br />
    A a{0};<br />
<br />
    a.b++; // Non-compliant<br />
<br />
    int c[3] = {1, 2, 3};<br />
<br />
    c[2]++; // Non-compliant<br />
<br />
    A* const a_ptr = &amp;a;<br />
<br />
    a_ptr-&gt;b++; // Non-compliant<br />
}</code></div></div><br />
Thanks!]]></description>
			<content:encoded><![CDATA[Hi!<br />
<br />
The rule states <br />
<br />
<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>The increment (++) and decrement (--) operators should not be mixed with other operators in an expression.</blockquote>
<br />
<br />
The code checker we employ also flags the usage of these operators in expressions that contain member-access operators, subscript operators, and the like.<br />
<br />
The rationale and example appear to focus on other <span style="font-weight: bold;" class="mycode_b">arithmetic </span>operators, which makes sense.<br />
<br />
Is this an oversight in the rule formulation (missing "arithmetic"), or is the following intentionally non-compliant with 5-2-10?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>void foo()<br />
{<br />
    struct A { int b; };<br />
<br />
    A a{0};<br />
<br />
    a.b++; // Non-compliant<br />
<br />
    int c[3] = {1, 2, 3};<br />
<br />
    c[2]++; // Non-compliant<br />
<br />
    A* const a_ptr = &amp;a;<br />
<br />
    a_ptr-&gt;b++; // Non-compliant<br />
}</code></div></div><br />
Thanks!]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 5-0-6 about underlying type of boolean bit field]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1610</link>
			<pubDate>Fri, 01 Apr 2022 09:08:50 +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=1610</guid>
			<description><![CDATA[<blockquote class="mycode_quote"><cite>Quote:</cite>Rule 5–0–6 (Required) An implicit integral or floating-point conversion shall not reduce the size of the underlying type.</blockquote>
<br />
Refer to the <span style="font-style: italic;" class="mycode_i">Bit-field operands </span>part of <span style="font-style: italic;" class="mycode_i">determination of the underlying type of an expression</span>, <br />
<blockquote class="mycode_quote"><cite>Quote:</cite>Bit-field objects have an underlying type equivalent to an integral type of a size determined by their width. For example, a bit-field with width n, will have the same underlying type as a fundamental type with the same sign and width (if one exists).</blockquote>
<br />
However, for one bit field, there's no any fundamental type with only one bit.<br />
So what exactly are <span style="font-weight: bold;" class="mycode_b">bitA</span>s' and <span style="font-weight: bold;" class="mycode_b">bitB</span>s' underlying types? Please help to clarify, thanks.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct<br />
{<br />
    bool bitA : 1;<br />
&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;&nbsp;bitB : 1;<br />
} boolbitfield;<br />
<br />
int main()<br />
{<br />
    bool b = boolbitfield.bitA; // compliant or non-compliant?<br />
}</code></div></div>]]></description>
			<content:encoded><![CDATA[<blockquote class="mycode_quote"><cite>Quote:</cite>Rule 5–0–6 (Required) An implicit integral or floating-point conversion shall not reduce the size of the underlying type.</blockquote>
<br />
Refer to the <span style="font-style: italic;" class="mycode_i">Bit-field operands </span>part of <span style="font-style: italic;" class="mycode_i">determination of the underlying type of an expression</span>, <br />
<blockquote class="mycode_quote"><cite>Quote:</cite>Bit-field objects have an underlying type equivalent to an integral type of a size determined by their width. For example, a bit-field with width n, will have the same underlying type as a fundamental type with the same sign and width (if one exists).</blockquote>
<br />
However, for one bit field, there's no any fundamental type with only one bit.<br />
So what exactly are <span style="font-weight: bold;" class="mycode_b">bitA</span>s' and <span style="font-weight: bold;" class="mycode_b">bitB</span>s' underlying types? Please help to clarify, thanks.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct<br />
{<br />
    bool bitA : 1;<br />
&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;&nbsp;bitB : 1;<br />
} boolbitfield;<br />
<br />
int main()<br />
{<br />
    bool b = boolbitfield.bitA; // compliant or non-compliant?<br />
}</code></div></div>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[M5-0-15 + range-based for loops]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1599</link>
			<pubDate>Tue, 15 Feb 2022 12:25: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=1599</guid>
			<description><![CDATA[Hi,<br />
<br />
Our static analyzer is reporting M5-0-15 "this is using pointer arithmetic" in the following code:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>int x[3] = {1,2,3};<br />
<br />
int z = 0;<br />
for (int y : x)&nbsp;&nbsp;// M5-0-15 reported here<br />
{<br />
    z += y;<br />
}</code></div></div><br />
Most likely due to the range-based for loop expanding to an iterator-based for loop.<br />
<br />
However, we would argue the MISRA rules apply to code written by users, not code generated by the compiler behind the scenes. <br />
<br />
What do you think, is the above code violating M5-0-15?<br />
<br />
Thanks!]]></description>
			<content:encoded><![CDATA[Hi,<br />
<br />
Our static analyzer is reporting M5-0-15 "this is using pointer arithmetic" in the following code:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>int x[3] = {1,2,3};<br />
<br />
int z = 0;<br />
for (int y : x)&nbsp;&nbsp;// M5-0-15 reported here<br />
{<br />
    z += y;<br />
}</code></div></div><br />
Most likely due to the range-based for loop expanding to an iterator-based for loop.<br />
<br />
However, we would argue the MISRA rules apply to code written by users, not code generated by the compiler behind the scenes. <br />
<br />
What do you think, is the above code violating M5-0-15?<br />
<br />
Thanks!]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[5-2-12 - Does the rule apply for braced-init-list arg to initializer_list parameter?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1591</link>
			<pubDate>Wed, 24 Nov 2021 15:23:30 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5758">DavidFriberg</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1591</guid>
			<description><![CDATA[<span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="color: #333333;" class="mycode_color">Hi,</span></span></span><br />
<br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font">Rule 5-2-12 covers array to pointer decay in a function call:</span></span></span><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: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b"><span style="color: #333333;" class="mycode_color">Rule 5-2-12 (required, implementation, automated) </span></span><span style="color: #333333;" class="mycode_color">An identifier with array type passed as a function argument shall not </span><span style="color: #333333;" class="mycode_color">decay to a pointer.</span></span></span></span></blockquote>
<br />
<span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font">This rule has been brought over to the AUTOSAR C++14 guidelines in verbatim (as M5-2-12), where it applies to more modern C++.</span></span><br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><br />
Now, an object of type std::initializer_list&lt;E&gt; (for some type E) is, as per <a href="https://timsong-cpp.github.io/cppwp/n4140/dcl.init.list#5" target="_blank" rel="noopener" class="mycode_url">[dcl.init.list]/5</a> (N4140/C++14), defined to be constructible from a braced-init-list as follows:</span></span></span><br />
<blockquote class="mycode_quote"><cite>Quote:</cite><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: Arial;" class="mycode_font">An object of type std::initializer_list&lt;E&gt; is constructed from an initializer list as if the implementation allocated a temporary array of N elements of type const E, where N is the number of elements in the initializer list. Each element of that array is copy-initialized with the corresponding element of the initializer list, and the std::initializer_list&lt;E&gt; object is constructed to refer to that array.</span></span></span></span></blockquote>
<br />
<span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="color: #333333;" class="mycode_color">Some compilers (correctly within their implementation freedom) implements the constructor of their std::initializer_list class template with two arguments: a begin iterator for the "as if an array" argument, and a size of the list (automagically provided). </span></span></span><br />
<br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font">Our static analyzer for <span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font">AUTOSAR C++14 </span></span></span>(whilst noting that this is a pre-C++11 rule) uses this to argue that any occasion where a braced-init-list is used as argument to a (compatible) function parameter that is a specialization of std::initializer_list is a violation of M5-2-12, essentially meaning we can neither use any of the std::initializer_list constructors from our in-house container classes, nor act as clients for many of the standard library's container classes (say under the assumption that we have an certified subset STL available):</span></span></span><br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include &lt;initializer_list&gt;<br />
#include &lt;vector&gt;<br />
<br />
namespace rule_m5_2_12<br />
{<br />
<br />
/// @brief Docs.<br />
template &lt;typename ScalarT, std::int32_t N&gt;<br />
class StaticVector final<br />
{<br />
 public:<br />
    /// @brief Docs.<br />
    /// @param list Docs.<br />
    StaticVector(std::initializer_list&lt;ScalarT&gt; const list) noexcept : data_{} {}<br />
 private:<br />
    /// Docs.<br />
    ScalarT data_[N];<br />
};<br />
<br />
inline void f()<br />
{<br />
    constexpr std::int32_t kSize{3};<br />
<br />
    // "Array type is used as a pointer type argument in the function call."<br />
    auto const a = StaticVector&lt;std::int32_t, kSize&gt;{1, 2, 3};  // M5-2-12 violation<br />
    auto const b = std::vector&lt;std::int32_t&gt;({1, 2, 3});        // M5-2-12 violation<br />
<br />
    // ...<br />
}<br />
<br />
}  // namespace rule_m5_2_12</code></div></div><br />
<span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="color: #333333;" class="mycode_color">Is the intent that Rule M5-2-12 (in the modern C++ world) should prohibit any client side (braced-init-list argument) usage of std::initializer_list&lt;E&gt; function parameters?</span></span></span><br />
<br />
<span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="color: #333333;" class="mycode_color">Thanks!</span></span></span>]]></description>
			<content:encoded><![CDATA[<span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="color: #333333;" class="mycode_color">Hi,</span></span></span><br />
<br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font">Rule 5-2-12 covers array to pointer decay in a function call:</span></span></span><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: Arial;" class="mycode_font"><span style="font-weight: bold;" class="mycode_b"><span style="color: #333333;" class="mycode_color">Rule 5-2-12 (required, implementation, automated) </span></span><span style="color: #333333;" class="mycode_color">An identifier with array type passed as a function argument shall not </span><span style="color: #333333;" class="mycode_color">decay to a pointer.</span></span></span></span></blockquote>
<br />
<span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font">This rule has been brought over to the AUTOSAR C++14 guidelines in verbatim (as M5-2-12), where it applies to more modern C++.</span></span><br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><br />
Now, an object of type std::initializer_list&lt;E&gt; (for some type E) is, as per <a href="https://timsong-cpp.github.io/cppwp/n4140/dcl.init.list#5" target="_blank" rel="noopener" class="mycode_url">[dcl.init.list]/5</a> (N4140/C++14), defined to be constructible from a braced-init-list as follows:</span></span></span><br />
<blockquote class="mycode_quote"><cite>Quote:</cite><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: Arial;" class="mycode_font">An object of type std::initializer_list&lt;E&gt; is constructed from an initializer list as if the implementation allocated a temporary array of N elements of type const E, where N is the number of elements in the initializer list. Each element of that array is copy-initialized with the corresponding element of the initializer list, and the std::initializer_list&lt;E&gt; object is constructed to refer to that array.</span></span></span></span></blockquote>
<br />
<span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="color: #333333;" class="mycode_color">Some compilers (correctly within their implementation freedom) implements the constructor of their std::initializer_list class template with two arguments: a begin iterator for the "as if an array" argument, and a size of the list (automagically provided). </span></span></span><br />
<br />
<span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font">Our static analyzer for <span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font">AUTOSAR C++14 </span></span></span>(whilst noting that this is a pre-C++11 rule) uses this to argue that any occasion where a braced-init-list is used as argument to a (compatible) function parameter that is a specialization of std::initializer_list is a violation of M5-2-12, essentially meaning we can neither use any of the std::initializer_list constructors from our in-house container classes, nor act as clients for many of the standard library's container classes (say under the assumption that we have an certified subset STL available):</span></span></span><br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include &lt;initializer_list&gt;<br />
#include &lt;vector&gt;<br />
<br />
namespace rule_m5_2_12<br />
{<br />
<br />
/// @brief Docs.<br />
template &lt;typename ScalarT, std::int32_t N&gt;<br />
class StaticVector final<br />
{<br />
 public:<br />
    /// @brief Docs.<br />
    /// @param list Docs.<br />
    StaticVector(std::initializer_list&lt;ScalarT&gt; const list) noexcept : data_{} {}<br />
 private:<br />
    /// Docs.<br />
    ScalarT data_[N];<br />
};<br />
<br />
inline void f()<br />
{<br />
    constexpr std::int32_t kSize{3};<br />
<br />
    // "Array type is used as a pointer type argument in the function call."<br />
    auto const a = StaticVector&lt;std::int32_t, kSize&gt;{1, 2, 3};  // M5-2-12 violation<br />
    auto const b = std::vector&lt;std::int32_t&gt;({1, 2, 3});        // M5-2-12 violation<br />
<br />
    // ...<br />
}<br />
<br />
}  // namespace rule_m5_2_12</code></div></div><br />
<span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="color: #333333;" class="mycode_color">Is the intent that Rule M5-2-12 (in the modern C++ world) should prohibit any client side (braced-init-list argument) usage of std::initializer_list&lt;E&gt; function parameters?</span></span></span><br />
<br />
<span style="font-size: small;" class="mycode_size"><span style="font-family: Arial;" class="mycode_font"><span style="color: #333333;" class="mycode_color">Thanks!</span></span></span>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Essential types of constant variables]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1578</link>
			<pubDate>Fri, 30 Jul 2021 22:27:18 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5392">LordMordac</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1578</guid>
			<description><![CDATA[I'm having a disagreement with my tool vendor over the essential types of constant variables.<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>uint32_t v1 = 1U;<br />
const uint32_t v2 = 1U;<br />
static const uint32_t v3 = 1U;<br />
constexpr uint32_t v4 = 1U;</code></div></div><br />
My interpretation of the spec is that all the identifiers (v1, v2, v3 and v4) have uint32_t essential type.<br />
<br />
My tool vendor's interpretation is that v1 has a uint32_t essential type. While v2, v3 and v4 have a uint8_t essential type.  (I don't have any insight into their reasoning)<br />
<br />
Clarification on this issue would be much appreciated.]]></description>
			<content:encoded><![CDATA[I'm having a disagreement with my tool vendor over the essential types of constant variables.<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>uint32_t v1 = 1U;<br />
const uint32_t v2 = 1U;<br />
static const uint32_t v3 = 1U;<br />
constexpr uint32_t v4 = 1U;</code></div></div><br />
My interpretation of the spec is that all the identifiers (v1, v2, v3 and v4) have uint32_t essential type.<br />
<br />
My tool vendor's interpretation is that v1 has a uint32_t essential type. While v2, v3 and v4 have a uint8_t essential type.  (I don't have any insight into their reasoning)<br />
<br />
Clarification on this issue would be much appreciated.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Clarification for Rule 5-2-4]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1500</link>
			<pubDate>Fri, 26 Jul 2019 14:06:07 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4847">abgs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1500</guid>
			<description><![CDATA[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?<br />
<br />
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)?<br />
<br />
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]?<br />
<br />
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?<br />
<br />
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?]]></description>
			<content:encoded><![CDATA[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?<br />
<br />
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)?<br />
<br />
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]?<br />
<br />
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?<br />
<br />
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?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Underlying type's implicit conversion of the shift operators]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1497</link>
			<pubDate>Fri, 19 Jul 2019 11:49:26 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5299">chill</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1497</guid>
			<description><![CDATA[Is there a implicit conversion in shift expressions?<br />
<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>The underlying type of the result is the underlying type of the shift-expression.</blockquote>
<br />
[code]<br />
int8_t i8;<br />
uint8_t u8;<br />
uint32_t u32;<br />
int32_t i32;<br />
<br />
u32 + i8; // i8 -&gt; u32<br />
u32 += i8; // i8 -&gt; u32<br />
<br />
u32 ]]></description>
			<content:encoded><![CDATA[Is there a implicit conversion in shift expressions?<br />
<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>The underlying type of the result is the underlying type of the shift-expression.</blockquote>
<br />
[code]<br />
int8_t i8;<br />
uint8_t u8;<br />
uint32_t u32;<br />
int32_t i32;<br />
<br />
u32 + i8; // i8 -&gt; u32<br />
u32 += i8; // i8 -&gt; u32<br />
<br />
u32 ]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[About Rule 5-0-15 and the size of the array]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1481</link>
			<pubDate>Mon, 25 Mar 2019 17:32:17 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4892">Sebastien.Ashby@nxp</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1481</guid>
			<description><![CDATA[Hi,<br />
I have a question about Rule 5-0-15: "Array indexing shall be the only for of pointer arithmetic".<br />
<br />
Is the rule satisfied if the code referencing the array element has no knowledge of the dimensions of the array object at compile/link time?<br />
<br />
For example, in the case where the argument passed to parameter "p2" of "my_fn" is an array whose size is defined at runtime?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>void my_fn ( uint8_t * p1, uint8_t p2[ ] )<br />
{<br />
&nbsp;&nbsp; uint8_t index = 0;<br />
&nbsp;&nbsp; uint8_t * p3;<br />
&nbsp;&nbsp; uint8_t * p4;<br />
&nbsp;&nbsp; *p1 = 0;<br />
&nbsp;&nbsp; ++index;<br />
&nbsp;&nbsp; index = index + 5;<br />
&nbsp;&nbsp; p1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= p1 + 5;&nbsp;&nbsp; // Non-compliant â€“ pointer increment<br />
&nbsp;&nbsp; p1[ 5 ] = 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Non-compliant â€“ p1 was not declared as array<br />
&nbsp;&nbsp; p3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= &amp;p1[ 5 ]; // Non-compliant â€“ p1 was not declared as array<br />
<br />
&nbsp;&nbsp; p2[ 0 ]&nbsp;&nbsp;&nbsp;&nbsp; = 0;<br />
&nbsp;&nbsp; p2[ index ] = 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Compliant<br />
&nbsp;&nbsp; p4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= &amp;p2[ 5 ]; // Compliant<br />
}<br />
uint8_t a1[ 16 ];<br />
uint8_t a2[ 16 ];<br />
<br />
my_fn ( a1, a2 );<br />
my_fn ( &amp;a1[ 4 ], &amp;a2[ 4 ] );</code></div></div>]]></description>
			<content:encoded><![CDATA[Hi,<br />
I have a question about Rule 5-0-15: "Array indexing shall be the only for of pointer arithmetic".<br />
<br />
Is the rule satisfied if the code referencing the array element has no knowledge of the dimensions of the array object at compile/link time?<br />
<br />
For example, in the case where the argument passed to parameter "p2" of "my_fn" is an array whose size is defined at runtime?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>void my_fn ( uint8_t * p1, uint8_t p2[ ] )<br />
{<br />
&nbsp;&nbsp; uint8_t index = 0;<br />
&nbsp;&nbsp; uint8_t * p3;<br />
&nbsp;&nbsp; uint8_t * p4;<br />
&nbsp;&nbsp; *p1 = 0;<br />
&nbsp;&nbsp; ++index;<br />
&nbsp;&nbsp; index = index + 5;<br />
&nbsp;&nbsp; p1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= p1 + 5;&nbsp;&nbsp; // Non-compliant â€“ pointer increment<br />
&nbsp;&nbsp; p1[ 5 ] = 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Non-compliant â€“ p1 was not declared as array<br />
&nbsp;&nbsp; p3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= &amp;p1[ 5 ]; // Non-compliant â€“ p1 was not declared as array<br />
<br />
&nbsp;&nbsp; p2[ 0 ]&nbsp;&nbsp;&nbsp;&nbsp; = 0;<br />
&nbsp;&nbsp; p2[ index ] = 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Compliant<br />
&nbsp;&nbsp; p4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= &amp;p2[ 5 ]; // Compliant<br />
}<br />
uint8_t a1[ 16 ];<br />
uint8_t a2[ 16 ];<br />
<br />
my_fn ( a1, a2 );<br />
my_fn ( &amp;a1[ 4 ], &amp;a2[ 4 ] );</code></div></div>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[5-0-3: Meaning of "implicitly converted to a different underlying type"]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1469</link>
			<pubDate>Mon, 11 Feb 2019 16:00:22 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4847">abgs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1469</guid>
			<description><![CDATA[Rule 5-0-3 says "A cvalue expression shall not be implicitly converted to a different underlying type."<br />
<br />
The rationale goes on to say "an expression defined as a cvalue shall not undergo further implicit conversions".<br />
<br />
In the expression 's64 = s32 + s32', the addition is a cvalue expression of underlying type int32_t. It is being assigned to an object of type int64_t. The C++ standard says (regarding the assignment operator): "If the left operand is not of class type, the [align=right] expression is implicitly converted to the cv-unqualified type of the left operand". <br />
<br />
Does this "implicit conversion to the type of the left operand" qualify as causing the cvalue expression to be "implicitly converted to a different underlying type"?<br />
<br />
<br />
In the expression 's64 == s32 + s32' the addition is a cvalue expression of underlying type int32_t. The C++ standard says (regarding the equality operator): "The usual arithmetic conversions are performed on operands of arithmetic or enumeration type", the result of which will be that "the<br />
operand with the type of lesser integer conversion rank shall be converted to the type of the operand with greater rank". Additionally, MISRA C++ defines "underlying type conversions" and says they are "conceptual replacements for the usual arithmetic conversions". In this case, the "underlying type conversions" say: "... if both operands have integral type, the underlying type of the expression can be found using the following: ... the type of the result is that of the larger type". Whether the "usual arithmetic conversions" or the "underlying type conversions" are used, the result of the addition will be converted to int64_t.<br />
<br />
Does the application of the usual arithmetic conversions and the ensuing implicit conversion to the larger type qualify as causing the cvalue expression to be "implicitly converted to a different underlying type"?<br />
<br />
<br />
In the expression '0L == s32 + s32' (where long is int64_t and 0L is signed char as established <a href="https://www.misra.org.uk/forum/viewtopic.php?f=190&amp;t=1146" target="_blank" rel="noopener" class="mycode_url">here</a>) does the implicit conversion from the promotion of the addition expression to int64_t qualify as causing the cvalue expression to be "implicitly converted to a different underlying type"?<br />
<br />
<br />
Note that these examples intend to use s32 and 64 to avoid the operands to the addition operator undergoing sub-int promotion, i.e. s32 is 32-bit int and s64 is 64-bit long.]]></description>
			<content:encoded><![CDATA[Rule 5-0-3 says "A cvalue expression shall not be implicitly converted to a different underlying type."<br />
<br />
The rationale goes on to say "an expression defined as a cvalue shall not undergo further implicit conversions".<br />
<br />
In the expression 's64 = s32 + s32', the addition is a cvalue expression of underlying type int32_t. It is being assigned to an object of type int64_t. The C++ standard says (regarding the assignment operator): "If the left operand is not of class type, the [align=right] expression is implicitly converted to the cv-unqualified type of the left operand". <br />
<br />
Does this "implicit conversion to the type of the left operand" qualify as causing the cvalue expression to be "implicitly converted to a different underlying type"?<br />
<br />
<br />
In the expression 's64 == s32 + s32' the addition is a cvalue expression of underlying type int32_t. The C++ standard says (regarding the equality operator): "The usual arithmetic conversions are performed on operands of arithmetic or enumeration type", the result of which will be that "the<br />
operand with the type of lesser integer conversion rank shall be converted to the type of the operand with greater rank". Additionally, MISRA C++ defines "underlying type conversions" and says they are "conceptual replacements for the usual arithmetic conversions". In this case, the "underlying type conversions" say: "... if both operands have integral type, the underlying type of the expression can be found using the following: ... the type of the result is that of the larger type". Whether the "usual arithmetic conversions" or the "underlying type conversions" are used, the result of the addition will be converted to int64_t.<br />
<br />
Does the application of the usual arithmetic conversions and the ensuing implicit conversion to the larger type qualify as causing the cvalue expression to be "implicitly converted to a different underlying type"?<br />
<br />
<br />
In the expression '0L == s32 + s32' (where long is int64_t and 0L is signed char as established <a href="https://www.misra.org.uk/forum/viewtopic.php?f=190&amp;t=1146" target="_blank" rel="noopener" class="mycode_url">here</a>) does the implicit conversion from the promotion of the addition expression to int64_t qualify as causing the cvalue expression to be "implicitly converted to a different underlying type"?<br />
<br />
<br />
Note that these examples intend to use s32 and 64 to avoid the operands to the addition operator undergoing sub-int promotion, i.e. s32 is 32-bit int and s64 is 64-bit long.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Clarification for 5-0-20 (operands to a binary bitwise operator)]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1468</link>
			<pubDate>Tue, 05 Feb 2019 15:52:50 +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=1468</guid>
			<description><![CDATA[Hi,<br />
<br />
Just to clarify what seems to be a mistake in the implementation of the static analysis tool I use:<br />
According to the rationale, I believe that this rule does not refer to the shift operators, but only to following 6 operators:<br />
  &amp;<br />
  |<br />
  ^<br />
  &amp;=<br />
  |=<br />
   ^=<br />
Am I right about this?<br />
<br />
Thanks,<br />
Udi.]]></description>
			<content:encoded><![CDATA[Hi,<br />
<br />
Just to clarify what seems to be a mistake in the implementation of the static analysis tool I use:<br />
According to the rationale, I believe that this rule does not refer to the shift operators, but only to following 6 operators:<br />
  &amp;<br />
  |<br />
  ^<br />
  &amp;=<br />
  |=<br />
   ^=<br />
Am I right about this?<br />
<br />
Thanks,<br />
Udi.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 5-2-6 and dynamicaly loading (.dll / .so)]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1461</link>
			<pubDate>Tue, 18 Dec 2018 08:42:51 +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=1461</guid>
			<description><![CDATA[The rule forbids conversion between function pointer types.<br />
I believe that this rule should exempt a function retrieved with GetProcAddress / dlsym, as I believe this is not an undefined / unspecified behavior (or is it?).<br />
It might make sense to add a requirement to review and document these cases.]]></description>
			<content:encoded><![CDATA[The rule forbids conversion between function pointer types.<br />
I believe that this rule should exempt a function retrieved with GetProcAddress / dlsym, as I believe this is not an undefined / unspecified behavior (or is it?).<br />
It might make sense to add a requirement to review and document these cases.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[5-2-7 and void pointer conversions]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1437</link>
			<pubDate>Fri, 13 Jul 2018 02:32:55 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=318">rgamble</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1437</guid>
			<description><![CDATA[Are conversions between pointers to void and pointers to object types a violation of rule 5-2-7?  It isn't clear what "object with pointer type" means here (or "unrelated pointer type" for that matter).  Specifically, are either of the casts below a violation of Rule 5-2-7?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>void bar(int *ip, void *vp) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;ip = static_cast(vp);<br />
&nbsp;&nbsp;&nbsp;&nbsp;vp = static_cast(ip);<br />
}</code></div></div>]]></description>
			<content:encoded><![CDATA[Are conversions between pointers to void and pointers to object types a violation of rule 5-2-7?  It isn't clear what "object with pointer type" means here (or "unrelated pointer type" for that matter).  Specifically, are either of the casts below a violation of Rule 5-2-7?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>void bar(int *ip, void *vp) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;ip = static_cast(vp);<br />
&nbsp;&nbsp;&nbsp;&nbsp;vp = static_cast(ip);<br />
}</code></div></div>]]></content:encoded>
		</item>
	</channel>
</rss>