<?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.7 Declarations (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 13:24:51 +0000</pubDate>
		<generator>MyBB</generator>
		<item>
			<title><![CDATA[Clarification for 7-4-1]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1432</link>
			<pubDate>Mon, 02 Jul 2018 14:36:57 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5040">rrosier</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1432</guid>
			<description><![CDATA[Rule 7-4-1 states that all usage of assembler shall be documented.<br />
<br />
There are two ways that I could interpret this rule - and I was wondering which of these two was meant:<br />
<ol type="1" class="mycode_list"><li>That a cross-reference document is kept stating where assembly language is used.<br />
</li>
<li>That all assembly language is documented with pseudo-code (or similar) such that it could be re-implemented in assembly language for a different processor (or even C/C++).</li>
</ol>
<br />
Is there any clarification about this available?]]></description>
			<content:encoded><![CDATA[Rule 7-4-1 states that all usage of assembler shall be documented.<br />
<br />
There are two ways that I could interpret this rule - and I was wondering which of these two was meant:<br />
<ol type="1" class="mycode_list"><li>That a cross-reference document is kept stating where assembly language is used.<br />
</li>
<li>That all assembly language is documented with pseudo-code (or similar) such that it could be re-implemented in assembly language for a different processor (or even C/C++).</li>
</ol>
<br />
Is there any clarification about this available?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 7-5-3 and storing a reference for later use]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1421</link>
			<pubDate>Tue, 08 May 2018 13:12:51 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5040">rrosier</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1421</guid>
			<description><![CDATA[Hello,<br />
<br />
I am curious if Rule 7-5-3 is only applicable when a reference to a parameter that is passed by const reference is returned from a function, but also if that reference is stored and then used later.<br />
<br />
The rationale of the rule states that "It is <span style="font-style: italic;" class="mycode_i">implementation-defined behaviour</span> whether the reference parameter is a temporary object or a reference to the parameter." and then goes on to say that a temporary object will be destroyed when the function returns and that using an object after its destruction will lead to <span style="font-style: italic;" class="mycode_i">undefined behaviour</span>.<br />
<br />
Considering the following code:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>class TestConstRef<br />
{<br />
public:<br />
&nbsp;&nbsp;TestConstRef(const T&amp; aT) : mT(aT) {}<br />
&nbsp;&nbsp;~TestConstRef() {}<br />
<br />
private:<br />
&nbsp;&nbsp;const T&amp; mT;<br />
};</code></div></div><br />
If I understand the rule correctly, the parameter <span style="font-weight: bold;" class="mycode_b">aT</span> could be a temporary object which will be destroyed when the constructor exits and when the member reference <span style="font-weight: bold;" class="mycode_b">mT</span> is used later, the temporary object that it refers to may (depending on the implementation) no longer exist and that will lead to <span style="font-style: italic;" class="mycode_i">undefined behaviour</span>.<br />
<br />
Could I ask:<ul class="mycode_list"><li>Is this understanding correct?<br />
</li>
<li>Is so, why is only returning the reference explicitly covered by the rules, and not storing the reference as in the code snippet above?</li>
</ul>
]]></description>
			<content:encoded><![CDATA[Hello,<br />
<br />
I am curious if Rule 7-5-3 is only applicable when a reference to a parameter that is passed by const reference is returned from a function, but also if that reference is stored and then used later.<br />
<br />
The rationale of the rule states that "It is <span style="font-style: italic;" class="mycode_i">implementation-defined behaviour</span> whether the reference parameter is a temporary object or a reference to the parameter." and then goes on to say that a temporary object will be destroyed when the function returns and that using an object after its destruction will lead to <span style="font-style: italic;" class="mycode_i">undefined behaviour</span>.<br />
<br />
Considering the following code:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>class TestConstRef<br />
{<br />
public:<br />
&nbsp;&nbsp;TestConstRef(const T&amp; aT) : mT(aT) {}<br />
&nbsp;&nbsp;~TestConstRef() {}<br />
<br />
private:<br />
&nbsp;&nbsp;const T&amp; mT;<br />
};</code></div></div><br />
If I understand the rule correctly, the parameter <span style="font-weight: bold;" class="mycode_b">aT</span> could be a temporary object which will be destroyed when the constructor exits and when the member reference <span style="font-weight: bold;" class="mycode_b">mT</span> is used later, the temporary object that it refers to may (depending on the implementation) no longer exist and that will lead to <span style="font-style: italic;" class="mycode_i">undefined behaviour</span>.<br />
<br />
Could I ask:<ul class="mycode_list"><li>Is this understanding correct?<br />
</li>
<li>Is so, why is only returning the reference explicitly covered by the rules, and not storing the reference as in the code snippet above?</li>
</ul>
]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 7-5-4 and safety]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1387</link>
			<pubDate>Tue, 19 Dec 2017 07:13:44 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4389">dg1980</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1387</guid>
			<description><![CDATA[Hi,<br />
<br />
I was just reading this article about the Toyota incident (<a href="http://www.safetyresearch.net/Library/BarrSlides_FINAL_SCRUBBED.pdf" target="_blank" rel="noopener" class="mycode_url">http://www.safetyresearch.net/Library/Ba...RUBBED.pdf</a>) and was wondering why this rule is advisory compared to MISRA C 2004 and MISRA C 2012?]]></description>
			<content:encoded><![CDATA[Hi,<br />
<br />
I was just reading this article about the Toyota incident (<a href="http://www.safetyresearch.net/Library/BarrSlides_FINAL_SCRUBBED.pdf" target="_blank" rel="noopener" class="mycode_url">http://www.safetyresearch.net/Library/Ba...RUBBED.pdf</a>) and was wondering why this rule is advisory compared to MISRA C 2004 and MISRA C 2012?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Clarification for 7-4-3]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1328</link>
			<pubDate>Tue, 28 Mar 2017 18:52:00 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4710">mikearmagedon</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1328</guid>
			<description><![CDATA[Rule 7-4-3 states that "... assembly instructions shall be encapsulated and isolated in either <span style="text-decoration: underline;" class="mycode_u">assembler functions</span> or C++ functions.".<br />
<br />
Does <span style="text-decoration: underline;" class="mycode_u">assembler functions</span> refers to assembly directives (.func and .endfunc) used in a .S file or it refers to C++ functions that encapsulate the assembly instructions?<br />
<br />
Is the example compliant?  <br />
asm.S<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>.global foo<br />
.func foo<br />
foo:<br />
...<br />
.endfunc</code></div></div>main.cpp<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>extern "C" {<br />
&nbsp;&nbsp;void foo();<br />
}<br />
<br />
int main(void) {<br />
&nbsp;&nbsp;...<br />
&nbsp;&nbsp;foo();<br />
&nbsp;&nbsp;...<br />
}</code></div></div>]]></description>
			<content:encoded><![CDATA[Rule 7-4-3 states that "... assembly instructions shall be encapsulated and isolated in either <span style="text-decoration: underline;" class="mycode_u">assembler functions</span> or C++ functions.".<br />
<br />
Does <span style="text-decoration: underline;" class="mycode_u">assembler functions</span> refers to assembly directives (.func and .endfunc) used in a .S file or it refers to C++ functions that encapsulate the assembly instructions?<br />
<br />
Is the example compliant?  <br />
asm.S<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>.global foo<br />
.func foo<br />
foo:<br />
...<br />
.endfunc</code></div></div>main.cpp<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>extern "C" {<br />
&nbsp;&nbsp;void foo();<br />
}<br />
<br />
int main(void) {<br />
&nbsp;&nbsp;...<br />
&nbsp;&nbsp;foo();<br />
&nbsp;&nbsp;...<br />
}</code></div></div>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Clarification for 7-3-1: extern "C++"]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1241</link>
			<pubDate>Thu, 28 Apr 2016 16:21:36 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=3645">anj</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1241</guid>
			<description><![CDATA[For headers included by other C and C++ headers it is vital to use extern "C++".<br />
But the rule implicitly forbids it by only allowing 3 kind of top level declarations.<br />
<br />
Is it really intended to use this as the only compliant way:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>extern "C"<br />
{<br />
&nbsp;&nbsp;extern "C++"<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;namespace MY_API<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
}</code></div></div>]]></description>
			<content:encoded><![CDATA[For headers included by other C and C++ headers it is vital to use extern "C++".<br />
But the rule implicitly forbids it by only allowing 3 kind of top level declarations.<br />
<br />
Is it really intended to use this as the only compliant way:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>extern "C"<br />
{<br />
&nbsp;&nbsp;extern "C++"<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;namespace MY_API<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
}</code></div></div>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[7-3-1 declarations and definitions]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1102</link>
			<pubDate>Mon, 25 Aug 2014 16:33:41 +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=1102</guid>
			<description><![CDATA[Does rule 7-3-1 require a function declared at global scope be declared as "extern 'C'" for both the function's declaration and its definition or does one "extern 'C'" for that function suffice? For example, is this okay:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>extern "C" void f();<br />
void f() {}</code></div></div>]]></description>
			<content:encoded><![CDATA[Does rule 7-3-1 require a function declared at global scope be declared as "extern 'C'" for both the function's declaration and its definition or does one "extern 'C'" for that function suffice? For example, is this okay:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>extern "C" void f();<br />
void f() {}</code></div></div>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 7-3-6 Clarification]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1031</link>
			<pubDate>Thu, 27 Feb 2014 14:31:31 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=3152">melinda.bradury</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1031</guid>
			<description><![CDATA[Does this rule prevent "using namespace ..." in a header file, if, it is inside a namespace?<br />
<br />
e.g. is it ok to do this in the header...<br />
namespace ComponentX<br />
{<br />
using namespace BasicTypes;<br />
class AClass<br />
{<br />
    public:<br />
        bool_t IsActive(void);<br />
};<br />
}<br />
}<br />
<br />
or would i have to do this...       <br />
BasicTypes::bool_t IsActive(void);]]></description>
			<content:encoded><![CDATA[Does this rule prevent "using namespace ..." in a header file, if, it is inside a namespace?<br />
<br />
e.g. is it ok to do this in the header...<br />
namespace ComponentX<br />
{<br />
using namespace BasicTypes;<br />
class AClass<br />
{<br />
    public:<br />
        bool_t IsActive(void);<br />
};<br />
}<br />
}<br />
<br />
or would i have to do this...       <br />
BasicTypes::bool_t IsActive(void);]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Is rule 7-5-3 really relevant?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=828</link>
			<pubDate>Fri, 27 May 2011 10:24:38 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=1600">triboix</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=828</guid>
			<description><![CDATA[Hello,<br />
<br />
Is rule 7-5-3 really relevant?<br />
<br />
It says that something like<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>int&amp; f(int&amp; x)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;return x;<br />
}</code></div></div>is not compliant.<br />
<br />
But surely all C++ developers on the planet would expect x to be modified here after calling f:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>void f(int&amp; x)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;x++;<br />
}</code></div></div><br />
So why returning x directly would be an issue? I guess there is probably something I am not seeing here!<br />
<br />
Thanks for any clarification!<br />
<br />
  Fabrice]]></description>
			<content:encoded><![CDATA[Hello,<br />
<br />
Is rule 7-5-3 really relevant?<br />
<br />
It says that something like<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>int&amp; f(int&amp; x)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;return x;<br />
}</code></div></div>is not compliant.<br />
<br />
But surely all C++ developers on the planet would expect x to be modified here after calling f:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>void f(int&amp; x)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;x++;<br />
}</code></div></div><br />
So why returning x directly would be an issue? I guess there is probably something I am not seeing here!<br />
<br />
Thanks for any clarification!<br />
<br />
  Fabrice]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Clarification for 7â€“5â€“1]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=607</link>
			<pubDate>Mon, 12 Jan 2009 09:48:06 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=368">pkruk</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=607</guid>
			<description><![CDATA[<blockquote class="mycode_quote"><cite>Quote:</cite>A function shall not return a reference or a pointer to an automatic variable (including parameters), defined within the function.</blockquote>
<br />
I have a question about reference types. Is the following code compliant or not?<br />
<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>int global_var;<br />
<br />
int &amp; foo(int &amp; ref) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;return ref;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Compliant?<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;int &amp; global_ref = global_var;<br />
&nbsp;&nbsp;&nbsp;&nbsp;return global_ref;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Compliant?<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;int local_var;<br />
&nbsp;&nbsp;&nbsp;&nbsp;int &amp; local_ref = local_var;<br />
&nbsp;&nbsp;&nbsp;&nbsp;return local_ref;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Compliant?<br />
}<br />
<br />
int * bar(int &amp; ref) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;return &amp;ref;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Compliant?<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;int &amp; global_ref = global_var;<br />
&nbsp;&nbsp;&nbsp;&nbsp;return &amp;global_ref;&nbsp;&nbsp;&nbsp;&nbsp; // Compliant?<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;int local_var;<br />
&nbsp;&nbsp;&nbsp;&nbsp;int &amp; local_ref = local_var;<br />
&nbsp;&nbsp;&nbsp;&nbsp;return &amp;local_ref;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Compliant?<br />
}</code></div></div>]]></description>
			<content:encoded><![CDATA[<blockquote class="mycode_quote"><cite>Quote:</cite>A function shall not return a reference or a pointer to an automatic variable (including parameters), defined within the function.</blockquote>
<br />
I have a question about reference types. Is the following code compliant or not?<br />
<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>int global_var;<br />
<br />
int &amp; foo(int &amp; ref) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;return ref;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Compliant?<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;int &amp; global_ref = global_var;<br />
&nbsp;&nbsp;&nbsp;&nbsp;return global_ref;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Compliant?<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;int local_var;<br />
&nbsp;&nbsp;&nbsp;&nbsp;int &amp; local_ref = local_var;<br />
&nbsp;&nbsp;&nbsp;&nbsp;return local_ref;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Compliant?<br />
}<br />
<br />
int * bar(int &amp; ref) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;return &amp;ref;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Compliant?<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;int &amp; global_ref = global_var;<br />
&nbsp;&nbsp;&nbsp;&nbsp;return &amp;global_ref;&nbsp;&nbsp;&nbsp;&nbsp; // Compliant?<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;int local_var;<br />
&nbsp;&nbsp;&nbsp;&nbsp;int &amp; local_ref = local_var;<br />
&nbsp;&nbsp;&nbsp;&nbsp;return &amp;local_ref;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Compliant?<br />
}</code></div></div>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Confusion over enum type expressions]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=569</link>
			<pubDate>Thu, 21 Aug 2008 18:57:44 +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=569</guid>
			<description><![CDATA[Hi,<br />
  The rationale for rule 7-2-1 states,<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>It is unspecified behaviour if the evaluation of an expression with enum underlying type yields a value which does not correspond to one of the enumerators of the enumeration.</blockquote>
This assertion contains a slight inaccuracy.  The relevant section of the ISO C++ (2003) standard, 7.2(9), <span style="font-style: italic;" class="mycode_i">actually</span> reads,<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>An expression of arithmetic or enumeration type can be converted to an enumeration type explicitly. <span style="font-weight: bold;" class="mycode_b">The value is unchanged if it is in the range of enumeration values of the enumeration type; otherwise the resulting enumeration value is unspecified</span>.</blockquote>
(emphasis added by this Poster).<br />
<br />
Should the MISRA C++ rule, instead, read,<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>An expression with enum underlying type shall only have values <span style="font-style: italic;" class="mycode_i">within the range of</span> the enumerators of the enumeration.</blockquote>
(emphasis, again, added by this Poster)?]]></description>
			<content:encoded><![CDATA[Hi,<br />
  The rationale for rule 7-2-1 states,<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>It is unspecified behaviour if the evaluation of an expression with enum underlying type yields a value which does not correspond to one of the enumerators of the enumeration.</blockquote>
This assertion contains a slight inaccuracy.  The relevant section of the ISO C++ (2003) standard, 7.2(9), <span style="font-style: italic;" class="mycode_i">actually</span> reads,<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>An expression of arithmetic or enumeration type can be converted to an enumeration type explicitly. <span style="font-weight: bold;" class="mycode_b">The value is unchanged if it is in the range of enumeration values of the enumeration type; otherwise the resulting enumeration value is unspecified</span>.</blockquote>
(emphasis added by this Poster).<br />
<br />
Should the MISRA C++ rule, instead, read,<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>An expression with enum underlying type shall only have values <span style="font-style: italic;" class="mycode_i">within the range of</span> the enumerators of the enumeration.</blockquote>
(emphasis, again, added by this Poster)?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Overloaded operators and MISRA C++]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=550</link>
			<pubDate>Thu, 10 Jul 2008 17:43:05 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=624">James Widman</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=550</guid>
			<description><![CDATA[I notice that MISRA C++ does not seem to say much about overloaded operators, and that surprises me a little.  I half-expected MISRA to refer to <a href="http://www.boost.org/doc/libs/1_35_0/libs/utility/operators.htm" target="_blank" rel="noopener" class="mycode_url">the Boost operators header</a>.<br />
<br />
Basically, it works like this:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>//////////////////////////////////<br />
// Boost operator header contains:<br />
namespace boost { <br />
&nbsp;&nbsp;&nbsp;&nbsp;template <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct addable1 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;friend T operator +( const T&amp; x, const T&amp; y ) <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ T t( x ); t += y; return t; } <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<br />
}<br />
<br />
/////////////////////////<br />
// Project code contains:<br />
struct B<br />
&nbsp;&nbsp;&nbsp;&nbsp;: boost::addable1<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;B&amp; operator+=( const B&amp; );<br />
};<br />
<br />
B f( B &amp;c, B&amp; d )<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;return c + d; // uses the implicitly-insatntiated friend of<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 'boost::addable1', which in turn uses<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 'B::operator+='.<br />
}</code></div></div>Compared to the rest of Boost, it's very easy to understand.  And it greatly simplifies the construction of a complete set of overloaded operators that behave in a canonical fashion.  So I think it could make a lot of sense to introduce an advisory rule that recommends the use of this header.<br />
<br />
But note that this whole scheme depends on the friend operator not being instantiated until its first use, and there is a defect report relevant to this: <a href="http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#329" target="_blank" rel="noopener" class="mycode_url">issue 329</a> (which has been resolved since late 2003).  The change given in the DR was not voted into the ISO C++ Working Paper until just after ISO C++2003 released.  However, the change to the standard was introduced to match the existing practice of compilers, so very few people (if any) are going to have a problem here.  And to find out whether you're one of those few, you only need to feed the above example to your compiler.<br />
<br />
Also  note, the above example is in violation of rule 14â€“6â€“2 because B::operator+= is declared after its use in the friend.  But, as I <a href="http://www.misra.org.uk/forum/viewtopic.php?f=198&amp;t=845&amp;sid=e7d29845b3458925c74130a2b3e3b005" target="_blank" rel="noopener" class="mycode_url">wrote before</a>, I think that rule should probably be removed from MISRA C++.]]></description>
			<content:encoded><![CDATA[I notice that MISRA C++ does not seem to say much about overloaded operators, and that surprises me a little.  I half-expected MISRA to refer to <a href="http://www.boost.org/doc/libs/1_35_0/libs/utility/operators.htm" target="_blank" rel="noopener" class="mycode_url">the Boost operators header</a>.<br />
<br />
Basically, it works like this:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>//////////////////////////////////<br />
// Boost operator header contains:<br />
namespace boost { <br />
&nbsp;&nbsp;&nbsp;&nbsp;template <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct addable1 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;friend T operator +( const T&amp; x, const T&amp; y ) <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ T t( x ); t += y; return t; } <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<br />
}<br />
<br />
/////////////////////////<br />
// Project code contains:<br />
struct B<br />
&nbsp;&nbsp;&nbsp;&nbsp;: boost::addable1<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;B&amp; operator+=( const B&amp; );<br />
};<br />
<br />
B f( B &amp;c, B&amp; d )<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;return c + d; // uses the implicitly-insatntiated friend of<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 'boost::addable1', which in turn uses<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 'B::operator+='.<br />
}</code></div></div>Compared to the rest of Boost, it's very easy to understand.  And it greatly simplifies the construction of a complete set of overloaded operators that behave in a canonical fashion.  So I think it could make a lot of sense to introduce an advisory rule that recommends the use of this header.<br />
<br />
But note that this whole scheme depends on the friend operator not being instantiated until its first use, and there is a defect report relevant to this: <a href="http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#329" target="_blank" rel="noopener" class="mycode_url">issue 329</a> (which has been resolved since late 2003).  The change given in the DR was not voted into the ISO C++ Working Paper until just after ISO C++2003 released.  However, the change to the standard was introduced to match the existing practice of compilers, so very few people (if any) are going to have a problem here.  And to find out whether you're one of those few, you only need to feed the above example to your compiler.<br />
<br />
Also  note, the above example is in violation of rule 14â€“6â€“2 because B::operator+= is declared after its use in the friend.  But, as I <a href="http://www.misra.org.uk/forum/viewtopic.php?f=198&amp;t=845&amp;sid=e7d29845b3458925c74130a2b3e3b005" target="_blank" rel="noopener" class="mycode_url">wrote before</a>, I think that rule should probably be removed from MISRA C++.]]></content:encoded>
		</item>
	</channel>
</rss>