AutosarC++18_03-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
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
It is undefined behavior, when a compiler sees the (partial or explicit) template specialization after it already has instantiated the primary or less specialized template.
Moreover, the case (2) allows compile-time interfaces to be extensible, as developers can safely provide custom specializations e.g. for traits classes or std::hash.
Example
// $Id: A14-7-2.cpp 312645 2018-03-21 11:44:35Z michal.szczepankiewicz $
#include <cstdint>
// in A.hpp
#include <functional>
struct A {
std::uint8_t x;
};
namespace std {
// compliant, case (2)
// template specialization for the user-defined type
// in the same file as the type declaration
template <> struct hash<A> {
size_t operator()(const A &a) const noexcept {
return std::hash<decltype(a.x)>()(a.x);
}
};
} // namespace std
// traits.hpp
#include <cstdint>
#include <type_traits>
template <typename T> struct is_serializable : std::false_type {};
// compliant, case (1)
template <> struct is_serializable<std::uint8_t> : std::true_type {};
// func.cpp
#include <vector>
// non-compliant, not declared
// in the same file as
// is_serializable class
template <> struct is_serializable<std::uint16_t> : std::true_type {};
template <typename T, typename = std::enable_if<is_serializable<T>::value>>
std::vector<std::uint8_t> serialize(const T &t) {
// only a basic stub
return std::vector<std::uint8_t>{t};
}
#include <string>
int main() { serialize(std::uint8_t{3}); }
See Also
- MISRA C++ 2008 [6]: Rule 14-7-2 (Required) For any given template specialization, an explicit instantiation of the template with the template arguments used in the specialization shall not render the program ill-formed.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
template_specialization_in_different_file |
Specialization not declared in same file as primary template |
None |
False |
Options¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
allow_in_directly_included_files¶
allow_in_directly_included_files : bool = False
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
only_fully_specialized_with_class¶
only_fully_specialized_with_class : bool = False
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