Function template explicitly specialized

ID

c.maintainability.function_template_specialized

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Design

Language

C / C++

Description

Reports an explicit specialization of a function template (template<> …​), declared or defined.

Rationale

Explicit function-template specializations do not participate in overload resolution the way ordinary overloads do: the compiler first picks the best-matching primary template based on the call site, and only then looks for a specialization matching that instantiation, so a specialization that is visible at one call site but not another silently changes behavior depending on include order — a well-known and hard-to-debug pitfall. A plain overload is resolved directly and does not have this ordering hazard.

The following code illustrates the pattern detected by this rule:

int max_of(int a, int b) {
    return a > b ? a : b;
}

// FLAGGED: Function template explicitly specialized
template <>
const char *max_of<const char *>(const char *a, const char *b) {
    return a;
}

Remediation

Prefer overloading the function directly for the specific type instead of specializing the template, and ensure the overload (or the specialization, if it is kept) is visible at every call site, ideally by declaring it alongside the primary template in the same header.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved