Main Content

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

The One Definition Rule shall not be violated

Description

This checker is deactivated in a default Polyspace® as You Code analysis. See Checkers Deactivated in Polyspace as You Code Analysis (Polyspace Access).

Rule Definition

The One Definition Rule shall not be violated.

Rationale

Violations of the One Definition Rule leads to undefined behavior.

Polyspace Implementation

The checker flags situations where the same function or object has multiple definitions and the definitions differ by some token. 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

Additional Message in Report

The One Definition Rule shall not be violated.

Declaration of class className violates the One Definition Rule:

it conflicts with other declaration (fileNamelineNumber).

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

This example uses two files:

  • file1.cpp:

    typedef struct S //Noncompliant
    {
       int x;
       int y;
    }S; 
    void foo(S& s){
    //...
    } 
  • file2.cpp:

    typedef struct S 
    {
       int y;
       int x;
    }S ; 
    void bar(S& s){
    //...
    }

In this example, both file1.cpp and file2.cpp define the structure S. However, the definitions switch the order of the structure fields.

Check Information

Group: Basic Concepts
Category: Required

Version History

Introduced in R2013b