在 MISRA C++ 2008 中,有人知道规则 5-0-3 中出现的特殊概念 Cvalue 表达式吗?
In MISRA C++ 2008, anyone knows the specialized notion Cvalue expression occured in the rule 5-0-3?
从Cvalue的概念,我认识到
"An expression that should not undergo further conversions, either implicitly or explicitly, is called a cvalue expression."
但是以这个规则给出的例子。
s32 = static_cast < int32_t > ( s8 ) + s8; // Example 2 - Compliant
s32 = s32 + s8; // Example 3 - Compliant
很明显,正确的加法表达式在这里发生了隐式和显式的转换。这条规则将它们标记为合规。我认为它与 cvalue 的概念相冲突。
在第 57 页末尾,MISRA Cpp 2008:
Similar, unless listed below:
. . .
The other unlisted expressions are not cvalues and have the underlying type of the operation.
阅读该段之后的长列表,没有任何内容可以应用于 s8
。
那么,s8
不是cvalues,它有操作的底层类型,在你的例子中,它的底层类型是int32_t
。升级到 int32_t
并不违反规则。
5-0-3 的重点是要确保所有操作都在相同的基础类型中执行。
int32_t s32;
int8_t s8;
s32 = static_cast < int32_t > ( s8 ) + s8; // Example 2 - Compliant
s32 = s32 + s8; // Example 3 - Compliant
在上面的例子中,+
是使用 int32_t
的基础类型执行的(到目前为止,大多数 int
是 int32_t
或 int16_t
), return int32_t
的基础类型,然后分配给 int32_t
变量,因此符合 MISRA Cpp.
在这个例子中:
int32_t s32;
int8_t s8;
s32 = s8 + s8;
不合规,因为加法运算符是用int
的类型进行的,结果会转为int32_t
,与int
没有必要,因此不合规。
从Cvalue的概念,我认识到
"An expression that should not undergo further conversions, either implicitly or explicitly, is called a cvalue expression."
但是以这个规则给出的例子。
s32 = static_cast < int32_t > ( s8 ) + s8; // Example 2 - Compliant
s32 = s32 + s8; // Example 3 - Compliant
很明显,正确的加法表达式在这里发生了隐式和显式的转换。这条规则将它们标记为合规。我认为它与 cvalue 的概念相冲突。
在第 57 页末尾,MISRA Cpp 2008:
Similar, unless listed below:
. . .
The other unlisted expressions are not cvalues and have the underlying type of the operation.
阅读该段之后的长列表,没有任何内容可以应用于 s8
。
那么,s8
不是cvalues,它有操作的底层类型,在你的例子中,它的底层类型是int32_t
。升级到 int32_t
并不违反规则。
5-0-3 的重点是要确保所有操作都在相同的基础类型中执行。
int32_t s32;
int8_t s8;
s32 = static_cast < int32_t > ( s8 ) + s8; // Example 2 - Compliant
s32 = s32 + s8; // Example 3 - Compliant
在上面的例子中,+
是使用 int32_t
的基础类型执行的(到目前为止,大多数 int
是 int32_t
或 int16_t
), return int32_t
的基础类型,然后分配给 int32_t
变量,因此符合 MISRA Cpp.
在这个例子中:
int32_t s32;
int8_t s8;
s32 = s8 + s8;
不合规,因为加法运算符是用int
的类型进行的,结果会转为int32_t
,与int
没有必要,因此不合规。