c++ - MISRA 5-2-1规则需要后缀表达式吗?

标签 c++ misra

MISRA-2008中的规则5-2-1指出:

Each operand of a logical && or || shall be a postfix-expression. Exception: Where an expression consists of either a sequence of only logical && or a sequence of only logical ||, extra parentheses are not required.


以下是示例,直接来自文档本身:
if (x == 0 && ishigh)      // Non-compliant
if (( x == 0 ) && ishigh)  // Compliant
if (x || y || z)           // Compliant by exception, if x, y and z bool
if (x || y && z)           // Non-compliant
if (x || (y && z))         // Compliant
if (x && !y)               // Non-compliant
if (x && (!y))             // Compliant
if (is_odd(y) && x)        // Compliant
if ((x > c1) && (y > c2)  && (z > c3))   // Compliant - exception
if ((x > c1) && (y > c2)  || (z > c3))   // Non-compliant
if ((x > c1) && ((y > c2) || (z > c3)))  // Compliant as extra() used
有人可以指出我 posfix -表达式在哪里吗?他们看起来像主要的。我看不到++--之类的东西。

最佳答案

所有的主表达式都是后缀表达式。在这些示例中,唯一不是主表达式的postfix-expression是is_odd(y)
后缀表达式的grammar为:

postfix-expression:

primary-expression
postfix-expression [ expr-or-braced-init-list ]
postfix-expression ( expression-listopt )
simple-type-specifier ( expression-listopt )
typename-specifier ( expression-listopt )
simple-type-specifier braced-init-list
typename-specifier braced-init-list
postfix-expression . templateopt id-expression
postfix-expression -> templateopt id-expression
postfix-expression ++
postfix-expression --
dynamic_­cast < type-id > ( expression )
static_­cast < type-id > ( expression )
reinterpret_­cast < type-id > ( expression )
const_­cast < type-id > ( expression )
typeid ( expression )
typeid ( type-id )




primary-expression:

literal
this
( expression )
id-expression
lambda-expression
fold-expression
requires-expression

关于c++ - MISRA 5-2-1规则需要后缀表达式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63115808/

相关文章:

c - 为什么在一个序列点内最多只能有一个具有 volatile 限定类型的读取访问?

c++ - 转换为科学计数法时出现 double 错误

c++ - MPI 收集/减少操作困惑?

c - Polyspace 警告系统函数使用系统定义的参数标志

c - 使用#define 为结构成员起别名

c - 避免 MISRA 警告,从嵌入式编码器生成自动化代码

c++ - Protobuf 版本冲突

c++ - 在 VS C++ 项目中创建目录

c++ - C++ 中的数组排序问题

c - MISRA C2012 :10. 示例代码中存在 8 违规