AutosarC++17_03-M14.7.3¶
All partial and explicit specializations for a template shall be declared in the same file as the declaration of their primary template
Required inputs: IR
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 */ };
Note
For legal reasons, this rule’s description is not part of the public documentation.