Clang error: no function template matches function template specialization A (err_function_template_spec_no_match)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: no function template matches function template specialization A
Type Error
Category Semantic Issue
Internal Id err_function_template_spec_no_match
Internal Message no function template matches function template specialization %0
Regular Expression (?:error|fatal error)\: no function template matches function template specialization (.*?)
First Commit 2009-09-25 3a923c2d3799 WIP implementation of explicit function template specialization. This

Description

The error is issued by the Clang compiler when an attempt is made to specialize a function template, but the compiler cannot find a matching template declaration for that specialization. This situation occurs when a program tries to define a specialization for a function template without providing a primary template that the specialization could modify. In C++, template specialization allows developers to create a specific implementation of a template for certain types or values. However, before specializing a template, a generic version of the template, known as the primary template, must exist. The error aims to highlight the disconnect between the specialization attempt and the absence of a corresponding primary template.

Function templates in C++ are used to write generic and type-safe functions that can operate on different data types without being rewritten for each type. A function template specialization is a version of the template that is customized for a particular set of types or values. The error typically indicates that either the primary template is missing, has not been included, or is not visible due to scoping rules or namespace issues.

It is crucial to ensure:

  • The primary template is defined and visible in the scope where the specialization is attempted.
  • The specialized function template is declared with the correct template arguments that match the primary template.
  • Namespace and scoping considerations are correctly managed to make the primary template accessible.

Addressing this error involves reviewing the code to ensure that the specialization is properly declared and matches an available primary function template. A common solution includes defining the primary template before any of its specializations or verifying that the inclusion and namespace are correctly handled, so the compiler can find the primary template.

 
AI Generated

Example

In the following example, a function template specialization for the function f is attempted without a prior declaration or definition of a primary template for f. The specialization is intended for double types, as indicated by the syntax template<> void f<double>();. However, since there is no visible primary template for f that accepts type parameters, the Clang compiler emits an error. This error indicates the absence of a matching function template that could be specialized for the specified type, highlighting the necessity for a primary template to exist before any specialization can be successfully declared.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
// Template specialization without original template definition

template<> void f<double>();
Compiler Output
<source>:3:17: error: no function template matches function template specialization 'f'


Clang Internals (17.0.6)

Git Commit Message

WIP implementation of explicit function template specialization. This
first implementation recognizes when a function declaration is an
explicit function template specialization (based on the presence of a
template<> header), performs template argument deduction + ambiguity
resolution to determine which template is being specialized, and hooks

There are many caveats here:
  - We completely and totally drop any explicitly-specified template
  arguments on the floor
  - We don't diagnose any of the extra semantic things that we should
  diagnose. 
  - I haven't looked to see that we're getting the right linkage for
  explicit specializations

On a happy note, this silences a bunch of errors that show up in
libstdc++'s <iostream>, although Clang still can't get through the
entire header.

llvm-svn: 82728

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaTemplate.cpp (line 9496)

/// Perform semantic analysis for the given function template
/// specialization.
///
/// This routine performs all of the semantic analysis required for an
/// explicit function template specialization. On successful completion,
/// the function declaration \p FD will become a function template
/// specialization.
///
/// \param FD the function declaration, which will be updated to become a
/// function template specialization.
///
/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
/// if any. Note that this may be valid info even when 0 arguments are
/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
/// as it anyway contains info on the angle brackets locations.
///
/// \param Previous the set of declarations that may be specialized by
/// this function specialization.
///
/// \param QualifiedFriend whether this is a lookup for a qualified friend
/// declaration with no explicit template argument list that might be
/// befriending a function template specialization.
bool Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, LookupResult &Previous, bool QualifiedFriend) {
  // ...
  UnresolvedSetIterator Result = getMostSpecialized(Candidates.begin(), Candidates.end(), FailedCandidates, FD->getLocation(), PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), PDiag(diag::err_function_template_spec_ambiguous) << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), PDiag(diag::note_function_template_spec_matched));

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp

  • clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp:44:8: error: no function template matches function template specialization 'f'