AutosarC++18_10-A14.8.2¶
Explicit specializations of function templates shall not be used
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
Specializations of function templates do not participate in overload resolution. They are only considered after their primary template has been selected during overload resolution. This is highly dependent on the declaration order of overloads and specializations and may be inconsistent with developer expectations.
A non-template function is always selected over a function template specialization if they are otherwise an equally good match, which also may be confusing for developers.
Function templates cannot be partially specialized, which may lead to troublesome implementations. If a partial specialization is required, then it is recommended to write a single function template that delegates to a class template (which can be partially specialized).
Example
// $Id: A14-8-2.cpp 312698 2018-03-21 13:17:36Z michal.szczepankiewicz $
#include <cstdint>
#include <memory>
#include <iostream>
template <typename T>
void F1(T t)
{
//compliant, (a)
std::cout << "(a)" << std::endl;
}
template <>
void F1<>(uint16_t* p)
{
//non-compliant
//(x), explicit specialization of
//(a), not (b), due to declaration
//order
std::cout << "(x)" << std::endl;
}
template <typename T>
void F1(T* p)
{
//compliant, (b), overloads (a)
std::cout << "(b)" << std::endl;
}
template <>
void F1<>(uint8_t* p)
{
//non-compliant
//(c), explicit specialization of (b)
std::cout << "(c)" << std::endl;
}
void F1(uint8_t* p)
{
//compliant
//(d), plain function, overloads with (a), (b)
//but not with (c)
std::cout << "(d)" << std::endl;
}
int main(void)
{
auto sp8 = std::make_unique<uint8_t>(3);
auto sp16 = std::make_unique<uint16_t>(3);
F1(sp8.get()); //calls (d), which might be
//confusing, but (c) is non-compliant
F1(sp16.get()); //calls (b), which might be
//confusing, but (b) is non-compliant
}
See Also
- MISRA C++ 2008 [7]: 14-8-1: Overloaded function templates shall not be explicitly specialized.
- MISRA C++ 2008 [7]: 14-8-2: The viable function set for a function call should either contain no function specializations, or only contain function specializations.
- HIC++ v4.0 [9]: 14.2.2: Do not explicitly specialize a function template that is overloaded with other templates.
- C++ Core Guidelines [11]: T.144: Don’t specialize function templates.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
specialization_of_overloaded_template |
Overloaded function templates shall not be explicitly specialized |
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
This rule has no individual options.