AutosarC++17_03-A3.1.1

It shall be possible to include any header file in multiple translation units without violating the One Definition Rule

Required inputs: IR

Rationale

A header file is a file that holds declarations used in more than one translation unit and acts as an interface between separately compiled parts of a program. A header file often contains classes, object declarations, enums, functions, inline functions, templates, typedefs, type aliases and macros.

In particular, a header file is not supposed to contain or produce definitions of global objects or functions that occupy storage, especially objects that are not declared “extern” or definitions of functions that are not declared “inline”.

Example
//% $Id: A3-1-1.hpp 271715 2017-03-23 10:13:51Z piotr.tanski $
#include <cstdint>
void f1();                            // Compliant
extern void f2();                     // Compliant
void f3() {}                          // Non-compliant
static inline void f4() {}            // Compliant
template <typename T> void f5(T) {}   // Compliant
std::int32_t a;                       // Non-compliant
extern std::int32_t b;                // Compliant
constexpr static std::int32_t c = 10; // Compliant
namespace ns {
constexpr static std::int32_t d = 100; // Compliant
const static std::int32_t e = 50;      // Compliant
static std::int32_t f;                 // Non-compliant
static void f6() noexcept;             // Non-compliant
}
See Also
  • MISRA C++ 2008 [6]: Rule 3-1-1 It shall be possible to include any header file in multiple translation units without violating the One Definition Rule.

Possible Messages

Key

Text

Severity

Disabled

external_function_definition_in_header

External function definition in header file.

None

False

external_static_field_def_in_header

External definition in header file.

None

False

external_variable_definition_in_header

External variable definition in header file.

None

False

internal_function_declaration_in_header

Internal function declaration in header file.

None

False

internal_function_definition_in_header

Function definition in header file (with internal linkage).

None

False

internal_static_field_def_in_header

Definition in header file (with internal linkage).

None

False

internal_variable_definition_in_header

Variable definition in header file (with internal linkage).

None

False

tentative_variable_definition_in_header

Tentative variable definition in header file.

None

False

Options

accept_const_fields

accept_const_fields : bool = False

Whether const-qualified static fields in header files should be tolerated if they have internal linkage. Has no effect on violations about external definitions.
 

accept_const_variables

accept_const_variables : bool = True

Whether global const-qualified variables in header files should be tolerated if they have internal linkage. Has no effect on violations about external definitions.
 

find_non_external_function_declaration

find_non_external_function_declaration : bool = True

Whether function declarations with non-external linkage should be reported.
 

report_tentative_definitions

report_tentative_definitions : bool = False

Whether tentative definitions should be reported as well.