Main Content

MISRA C++:2008 Rule 3-2-1

All declarations of an object or function shall have compatible types

Description

Rule Definition

All declarations of an object or function shall have compatible types.

Rationale

If the declarations of an object or function in two different translation units have incompatible types, the behavior is undefined.

Polyspace Implementation

Polyspace® considers two types to be compatible if they have the same size and signedness in the environment that you use. The checker is not raised on unused code such as

  • Noninstantiated templates

  • Uncalled static or extern functions

  • Uncalled and undefined local functions

  • Unused types and variables

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

file1.cpp

                  
                  
typedef          char         char_t;
typedef signed   short        int16_t;
typedef signed   long         int64_t;

namespace bar {
	int64_t a;
	int16_t c;  // Noncompliant

};

file2.cpp

                  
                  
typedef          char         char_t;
typedef signed   int          int32_t;

namespace bar {
	extern char_t c;
	extern int32_t a;
	void foo(void){
		++a;
		++c;
	}
}; 

In this example, the variable bar::c is defined as a char in file2.cpp and as a signed short in file1.cpp. In the target processor i386, the size of these types are not equal. Polyspace flags the definition of bar::c.

The variable bar::a is defined as a long in file1.cpp and as an int in file2.cpp. In the target processor i386, both int and long has a size of 32 bits. Because the definitions of bar::a is compatible in both files, Polyspace does not raise a flag.

Check Information

Group: Basic Concepts
Category: Required

Version History

Introduced in R2013b