<?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.8 Declarators (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:48 +0000</pubDate>
		<generator>MyBB</generator>
		<item>
			<title><![CDATA[Rule 8-4-4 Exception for stream manipulators?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1782</link>
			<pubDate>Mon, 07 Sep 2026 08:22: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=1782</guid>
			<description><![CDATA[Hi,<br />
<br />
Does Rule 8-4-4 imply that we have to write code like this? I do not see any exception for stream manipulators. Our static analyzer is flagging this case.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>std::cout &lt;&lt; "Hello World" &lt;&lt; &amp;std::endl;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^</code></div></div>]]></description>
			<content:encoded><![CDATA[Hi,<br />
<br />
Does Rule 8-4-4 imply that we have to write code like this? I do not see any exception for stream manipulators. Our static analyzer is flagging this case.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>std::cout &lt;&lt; "Hello World" &lt;&lt; &amp;std::endl;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^</code></div></div>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 8-5-2: what about strings?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1394</link>
			<pubDate>Wed, 10 Jan 2018 13:15:14 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4939">jocob</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1394</guid>
			<description><![CDATA[struct Test<br />
{<br />
  char str1[20];<br />
  char str2[20];<br />
};<br />
<br />
struct Test t =<br />
{<br />
  "Hello",<br />
  "World"<br />
};<br />
<br />
LDRA flags this with 397 S (Array initialisation has insufficient items.) violation of 8-5-2 (Braces shall be used to indicate and match the structure in the non-zero initialization of arrays and structures.)<br />
<br />
Looking at the details of the rule in the guidelines, I see why: "The non-zero initialization of arrays or structures requires an explicit initializer for each element."<br />
<br />
It seems there should be an exception for initializing a character array with the string syntax, since:<br />
a. the unused characters are implicitly null character 0<br />
b. there is no MISRA compliant way to explicitly add null characters to a "string"<br />
<br />
I think MISRA C 2004 has a similar problem, but I haven't looked at 2012 yet.]]></description>
			<content:encoded><![CDATA[struct Test<br />
{<br />
  char str1[20];<br />
  char str2[20];<br />
};<br />
<br />
struct Test t =<br />
{<br />
  "Hello",<br />
  "World"<br />
};<br />
<br />
LDRA flags this with 397 S (Array initialisation has insufficient items.) violation of 8-5-2 (Braces shall be used to indicate and match the structure in the non-zero initialization of arrays and structures.)<br />
<br />
Looking at the details of the rule in the guidelines, I see why: "The non-zero initialization of arrays or structures requires an explicit initializer for each element."<br />
<br />
It seems there should be an exception for initializing a character array with the string syntax, since:<br />
a. the unused characters are implicitly null character 0<br />
b. there is no MISRA compliant way to explicitly add null characters to a "string"<br />
<br />
I think MISRA C 2004 has a similar problem, but I haven't looked at 2012 yet.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 8-5-1 Must a non-static class member variable also explicitly initialized in constructor?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1322</link>
			<pubDate>Tue, 07 Mar 2017 08:22:02 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=1736">ProgCPlusPlus</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1322</guid>
			<description><![CDATA[The main statement of rule 8-5-1 is:<br />
<br />
<blockquote class="mycode_quote"><cite>MISRA C++:2008 Wrote:</cite>Each class constructor shall initialize all non-static members of its class.</blockquote>The example shows a compliant and a non compliant constructor of a class having only member variables of basic numerical type <span style="font-weight: bold;" class="mycode_b">int32_t</span>. It is clear that a member variable of a basic type must be initialized before first read access to avoid an undefined behavior.<br />
<br />
But what about non-static members of a class being of type class.<br />
<br />
Must a constructor of the class member variable explicitly called in the constructor to be compliant to this rule?<br />
<br />
Example:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>class A<br />
{<br />
&nbsp;&nbsp; public:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A ( ) : m_a( false )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bool GetmA ( void )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ( m_a );<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp; private:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bool m_a;<br />
};<br />
<br />
<br />
class B<br />
{<br />
&nbsp;&nbsp; public:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B ( ) : m_a( false )&nbsp;&nbsp;// Compliant or non-compliant because of<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // not explicitly calling constructor of m_b?<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B ( const bool a ) : m_a( a ), m_b()&nbsp;&nbsp;// Definitely compliant<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bool GetmA ( void )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ( m_a );<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bool GetmB (void )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return m_b.GetmA();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp; private:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bool m_a;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A&nbsp;&nbsp;&nbsp;&nbsp;m_b;<br />
};<br />
<br />
int main ( void )<br />
{<br />
&nbsp;&nbsp; B b;<br />
&nbsp;&nbsp; if ( b.GetmB() )<br />
&nbsp;&nbsp; {<br />
&nbsp;&nbsp; }<br />
}</code></div></div>The default constructor of <span style="font-weight: bold;" class="mycode_b">class A</span> for member variable <span style="font-weight: bold;" class="mycode_b">m_b</span> is in both constructors of <span style="font-weight: bold;" class="mycode_b">class B</span> called, but in default constructor of <span style="font-weight: bold;" class="mycode_b">class B</span> only implicit according to C++ standard.<br />
<br />
Is the default constructor of <span style="font-weight: bold;" class="mycode_b">class B</span> not explicitly calling default constructor of <span style="font-weight: bold;" class="mycode_b">class A</span> for member variable <span style="font-weight: bold;" class="mycode_b">m_b</span> compliant to rule 8-5-1?]]></description>
			<content:encoded><![CDATA[The main statement of rule 8-5-1 is:<br />
<br />
<blockquote class="mycode_quote"><cite>MISRA C++:2008 Wrote:</cite>Each class constructor shall initialize all non-static members of its class.</blockquote>The example shows a compliant and a non compliant constructor of a class having only member variables of basic numerical type <span style="font-weight: bold;" class="mycode_b">int32_t</span>. It is clear that a member variable of a basic type must be initialized before first read access to avoid an undefined behavior.<br />
<br />
But what about non-static members of a class being of type class.<br />
<br />
Must a constructor of the class member variable explicitly called in the constructor to be compliant to this rule?<br />
<br />
Example:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>class A<br />
{<br />
&nbsp;&nbsp; public:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A ( ) : m_a( false )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bool GetmA ( void )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ( m_a );<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp; private:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bool m_a;<br />
};<br />
<br />
<br />
class B<br />
{<br />
&nbsp;&nbsp; public:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B ( ) : m_a( false )&nbsp;&nbsp;// Compliant or non-compliant because of<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // not explicitly calling constructor of m_b?<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B ( const bool a ) : m_a( a ), m_b()&nbsp;&nbsp;// Definitely compliant<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bool GetmA ( void )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ( m_a );<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bool GetmB (void )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return m_b.GetmA();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp; private:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bool m_a;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A&nbsp;&nbsp;&nbsp;&nbsp;m_b;<br />
};<br />
<br />
int main ( void )<br />
{<br />
&nbsp;&nbsp; B b;<br />
&nbsp;&nbsp; if ( b.GetmB() )<br />
&nbsp;&nbsp; {<br />
&nbsp;&nbsp; }<br />
}</code></div></div>The default constructor of <span style="font-weight: bold;" class="mycode_b">class A</span> for member variable <span style="font-weight: bold;" class="mycode_b">m_b</span> is in both constructors of <span style="font-weight: bold;" class="mycode_b">class B</span> called, but in default constructor of <span style="font-weight: bold;" class="mycode_b">class B</span> only implicit according to C++ standard.<br />
<br />
Is the default constructor of <span style="font-weight: bold;" class="mycode_b">class B</span> not explicitly calling default constructor of <span style="font-weight: bold;" class="mycode_b">class A</span> for member variable <span style="font-weight: bold;" class="mycode_b">m_b</span> compliant to rule 8-5-1?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 8-5-2 what is "non-zero initialization"?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1308</link>
			<pubDate>Tue, 24 Jan 2017 14:21:53 +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=1308</guid>
			<description><![CDATA[C++ has the concepts:<ul class="mycode_list"><li>default initialization ("T var;")<br />
Calls the default constructor (if it exists); otherwise the memory is left uninitialized.<br />
<br />
</li>
<li>value initialization ("T var = {};")<br />
Calls the default constructor if it exists and is user-defined.<br />
Otherwise, performs zero-initialization and then calls the compiler-generated default constructor (if it exists).<br />
<br />
</li>
<li>zero initialization<br />
Roughly equivalent to a memset(..., 0, ...)</li>
</ul>
<br />
The term "non-zero initialization" is not defined anywhere.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct POD { int member1; int member2; };<br />
class UserDefinedCtor { public: UserDefinedCtor(); };<br />
struct CompilerDefinedCtor { int member1; UserDefinedCtor member2; };<br />
<br />
POD pod[3] = {}; // value initialization that uses zero initialization<br />
UserDefinedCtor udc[3] = {}; // value initialization but does not involve zero initialization<br />
CompilerDefinedCtor cdc[3] = {}; // value initialization that involves both zero initialization and constructor calls</code></div></div><br />
The declaration `pod` is clearly compliant with Rule 8-5-2.<br />
<br />
What about `udc`? If it is classified as "non-zero initialization", Rule 8-5-2 would require an explicit initializator for each element:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>UserDefinedCtor udc[3] = { UserDefinedCtor(), UserDefinedCtor(), UserDefinedCtor() };</code></div></div><br />
But maybe "zero initialization" in this rule does not refer to the C++ concept, but instead to the syntactic forms "{}", "{0}" and "{NULL}" ?]]></description>
			<content:encoded><![CDATA[C++ has the concepts:<ul class="mycode_list"><li>default initialization ("T var;")<br />
Calls the default constructor (if it exists); otherwise the memory is left uninitialized.<br />
<br />
</li>
<li>value initialization ("T var = {};")<br />
Calls the default constructor if it exists and is user-defined.<br />
Otherwise, performs zero-initialization and then calls the compiler-generated default constructor (if it exists).<br />
<br />
</li>
<li>zero initialization<br />
Roughly equivalent to a memset(..., 0, ...)</li>
</ul>
<br />
The term "non-zero initialization" is not defined anywhere.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct POD { int member1; int member2; };<br />
class UserDefinedCtor { public: UserDefinedCtor(); };<br />
struct CompilerDefinedCtor { int member1; UserDefinedCtor member2; };<br />
<br />
POD pod[3] = {}; // value initialization that uses zero initialization<br />
UserDefinedCtor udc[3] = {}; // value initialization but does not involve zero initialization<br />
CompilerDefinedCtor cdc[3] = {}; // value initialization that involves both zero initialization and constructor calls</code></div></div><br />
The declaration `pod` is clearly compliant with Rule 8-5-2.<br />
<br />
What about `udc`? If it is classified as "non-zero initialization", Rule 8-5-2 would require an explicit initializator for each element:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>UserDefinedCtor udc[3] = { UserDefinedCtor(), UserDefinedCtor(), UserDefinedCtor() };</code></div></div><br />
But maybe "zero initialization" in this rule does not refer to the C++ concept, but instead to the syntactic forms "{}", "{0}" and "{NULL}" ?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule	8-4-4 (Required) function identifier with & or ()]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1099</link>
			<pubDate>Wed, 13 Aug 2014 19:38:15 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=142">Achim Olaf Zacher</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1099</guid>
			<description><![CDATA[Is it intended to ban the use of<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>std::cout</code></div></div>]]></description>
			<content:encoded><![CDATA[Is it intended to ban the use of<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>std::cout</code></div></div>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[6.8.5 Initializers and POD]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=978</link>
			<pubDate>Wed, 04 Sep 2013 07:36:51 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=612">martin.dowie</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=978</guid>
			<description><![CDATA[Rule 8-5-1 (Required) "All variables shall have a defined value before they are used."<br />
<br />
My reading of this rule leads me to the conclusion that "POD" classes are effectively not allowed, since any user-defined constructor provided to give initial values to non-static member data results in the class no longer being POD. Example:<br />
<br />
[code]// Standard headers<br />
#include <br />
#include   // std::is_pod<br />
<br />
// User-defined structs...how many are POD?<br />
struct Trivial_Ctor            { int i;                                      };<br />
struct With_User_Defined_Ctor  { int i; With_User_Defined_Ctor()         { } };<br />
struct With_Initialised_Member { int i; With_Initialised_Member() : i(0) { } };<br />
<br />
int main()<br />
{<br />
	std::cout ]]></description>
			<content:encoded><![CDATA[Rule 8-5-1 (Required) "All variables shall have a defined value before they are used."<br />
<br />
My reading of this rule leads me to the conclusion that "POD" classes are effectively not allowed, since any user-defined constructor provided to give initial values to non-static member data results in the class no longer being POD. Example:<br />
<br />
[code]// Standard headers<br />
#include <br />
#include   // std::is_pod<br />
<br />
// User-defined structs...how many are POD?<br />
struct Trivial_Ctor            { int i;                                      };<br />
struct With_User_Defined_Ctor  { int i; With_User_Defined_Ctor()         { } };<br />
struct With_Initialised_Member { int i; With_Initialised_Member() : i(0) { } };<br />
<br />
int main()<br />
{<br />
	std::cout ]]></content:encoded>
		</item>
	</channel>
</rss>