☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

声明与实现应一致

6.12.2 ID_inconsistentDeclaration
目录 › next › previous

声明与实现在书写上应完全一致,否则极易引起误解,而且对同一对象或函数进行类型不兼容的声明,也会导致标准未定义的行为。

示例:

extern long n;   // Non-compliant, undefined behavior

int foo() {
    return n++;   // Undefined behavior
}

short n;

例中变量 n 有多处声明,但类型不一致,会导致标准未定义的行为。

又如:

int foo(int x);

typedef int type;
type foo(type x) {   // Non-compliant, confusing
    ....
}

例中在实现处为参数类型定义别名是不符合要求的,在允许重载的 C++ 代码中会引起更大的误解。

应改为:

typedef int type;
type foo(type x);

type foo(type x) {   // Compliant
    ....
}

相关

ID_inconsistentParamName

依据

ISO/IEC 9899:1999 6.2.7(2)-undefined ISO/IEC 9899:2011 6.2.7(2)-undefined

参考

MISRA C 2004 8.4 MISRA C 2012 8.3 MISRA C++ 2008 3-9-1 SEI CERT DCL40-C
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.