CERT.DCL.ODR.CLASS_REDEFINITION

Obey the one-definition rule for class/struct redefinition across translation units.

This checker is applicable only to the modern engine.

This checker detects when the same externally linked class, struct, or union has distinct definitions in different translation units. Such redefinitions violate the C++ one-definition rule and can result in undefined or inconsistent program behavior.

Vulnerability and risk

When a type has different definitions across translation units, different parts of the program may use incompatible layouts or member definitions for what is intended to be the same type. This can lead to incorrect behavior, memory corruption, or difficult-to-diagnose linker/runtime issues.

The checker compares the class/struct/union keyword and the ordered sequence of direct members to distinguish genuinely different definitions from identical duplicate definitions. Classes in anonymous namespaces, function-local types, class templates, and types in different named namespaces are excluded because they represent distinct entities.

Mitigation and prevention

Define each class, struct, or union once in a shared header and include that header wherever the type is needed. Avoid maintaining separate definitions of the same externally linked type across translation units.

Vulnerable code example

Copy
// a.cpp
struct S {
    int a;
};

// b.cpp
class S {
public:
    int a;
}; // CERT.DCL.ODR.CLASS_REDEFINITION: two distinct definitions of 'S'

In this noncompliant example, S has two different definitions across translation units. Although both definitions contain a member named a, one uses struct and the other uses class, so they are distinct definitions and violate the one-definition rule.

Fixed code example

Copy
// s.hpp — included identically by every translation unit
struct S {
    int a;
};

This compliant approach provides a single definition of S that is shared consistently across translation units.

External guidance