AutosarC++18_10-A14.7.2

Template specialization shall be declared in the same file (1) as the primary template (2) as a user-defined type, for which the specialization is declared

Required inputs: IR

Template specializations should be declared in the same file as the primary template declaration to keep related code together and avoid confusion about which specialization applies. When specializations are scattered across multiple files, it becomes difficult to understand which specialization will be used and increases the risk of accidentally creating duplicate specializations.
Bad code (specialization in different file):
// template.h
template<typename T>
class Widget { /* definition */ };

// widget_int.cpp
template<>  // ERROR: specialization in different file
class Widget<int> {
    void process();
};
Good code (specialization in same file as primary):
// template.h
template<typename T>
class Widget { /* definition */ };

template<>     // OK: specialization in same file as primary
class Widget<int> { /* specialized for int */ };

// other.cpp
#include "template.h"  // Includes both primary and specialization
Exception (template only declared in header):
// template.h
template<typename T>
class Widget;  // Forward declaration only

// template.cpp
template<typename T>
class Widget { /* definition in .cpp */ };

template<>
class Widget<int> { /* OK: may be in different file */ };

Possible Messages

Key

Text

Severity

Disabled

template_specialization_in_different_file

Specialization not declared in same file as primary template

None

False

Options

allow_in_directly_included_files

allow_in_directly_included_files : bool = False

This option allows to relax the expected file to be any file that is directly included from the primary file or a type declaration file if applicable via the option allow_in_type_declaration_file and its modification only_fully_specialized_with_class.
 

allow_in_type_declaration_file

allow_in_type_declaration_file : bool = True

Also allow specialications in the same file of a user-defined type for which the specialization is declared.
 

only_fully_specialized_with_class

only_fully_specialized_with_class : bool = False

If this option is set to true, only full specialications with a class name within the same file of the class are allowed. This option modifies the option allow_in_type_declaration_file and has no effect if allow_in_type_declaration_file is false.
 

relax_if_template_only_declared

relax_if_template_only_declared : bool = False

Allow specializations that are not declared in the header file if the template itself is only declared but not defined in the header.