MISRA-C:2004 规则 10.1 不允许隐式扩展函数参数 或返回表达式 的类型,如中所示以下代码片段:
void foo1(int16_t x);
int16_t foo2(void)
{
int8_t s8a;
...
foo1(s8a); /* not compliant */
...
return s8a; /* not compliant */
}
但是,在我的理解中,它们与分配情况没有什么不同:
s16a = s8a; /* compliant */
有什么意义?谢谢。
最佳答案
MISRA-C:2004 规则 10.1(引用的指南)指出:
The value of an expression of integer type shall not be implicitly converted to a different underlying type if:
- it is not a conversion to a wider integer type of the same signedness, or
- ...
在引用的示例中,转换是到更宽的整数类型(int8_t 到 int16_t),因此规则 10.1 不适用。
(10.1 和 10.2 的)扩展说明该规则的目的是防止从较宽类型到较窄类型的隐式转换。反之则没有限制!
-- 编辑添加--
作为更新,MISRA-C:2004 规则 10.1 分布在 MISRA C:2012 的多个规则中……映射表(附录 1)包括评论:
Relaxed to permit implicit widening conversions on function arguments or return values.
因此,对于 MISRA C:2012,这不再是违规行为。如果偏离 2004 年规则(恕我直言,这是正确的方法),您可能希望考虑这一点。
关于c - 为什么 MISRA-C 在某些情况下不允许隐式扩大类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52870121/