Clang error: default argument not permitted on an explicit ... of function B (err_template_spec_default_arg)

From emmtrix Wiki
Jump to navigation Jump to search
Text
error: default argument not permitted on an explicit
instantiation
specialization
of function B

Type Error
Category Semantic Issue
Internal Id err_template_spec_default_arg
Internal Message default argument not permitted on an explicit %select{instantiation|specialization}0 of function %1
Regular Expression (?:error|fatal error)\: default argument not permitted on an explicit (?:instantiation|specialization) of function (.*?)
First Commit 2009-10-13 62e10f0bdcaa Diagnose attempts to add default function arguments to a

Description

The error is issued by the Clang compiler when a default argument is applied to an explicit instantiation or specialization of a template function. This situation arises under the rules of the C++ language, which prohibit adding default arguments in these contexts. Specifically, when a template function is explicitly specialized or instantiated, all parameters must be provided without default values. This ensures that the interface and behavior of the template are explicitly defined, preventing any ambiguity or unexpected behaviors due to default arguments. This error serves to enforce strict adherence to these rules, promoting clarity and consistency in template usage.  
AI Generated

Example

In the following example, a template function f with a single type parameter T is declared. Subsequently, an explicit specialization of the template for the type int is provided. In this specialization, an attempt is made to assign a default argument to the specialized function's parameter, which is not allowed in C++ for explicit specializations or instantiations of template functions. The compiler error message generated by Clang indicates this misuse by pointing out that a default argument is not permitted on an explicit specialization of the function f. This example demonstrates the enforcement of rules pertaining to template functions in C++, specifically the prohibition of default arguments in explicit specializations and instantiations.  
AI Generated


Flags -xc++ -std=c++17

[Try out in Compiler Explorer]

Source
template <typename T> void f(T t); // template declaration

template <> void f<int>(int t = 0); // explicit specialization with default argument
Compiler Output
<source>:3:29: error: default argument not permitted on an explicit specialization of function 'f'


Clang Internals (17.0.6)

Git Commit Message

Diagnose attempts to add default function arguments to a
specialization. This completes C++ [temp.expl.spec]!

llvm-svn: 83980

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/SemaDeclCXX.cpp (line 617)

/// MergeCXXFunctionDecl - Merge two declarations of the same C++
/// function, once we already know that they have the same
/// type. Subroutine of MergeFunctionDecl. Returns true if there was an
/// error, false otherwise.
bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old, Scope *S) {
  // ...
  // C++ [dcl.fct.default]p4:
  //   For non-template functions, default arguments can be added in
  //   later declarations of a function in the same
  //   scope. Declarations in different scopes have completely
  //   distinct sets of default arguments. That is, declarations in
  //   inner scopes do not acquire default arguments from
  //   declarations in outer scopes, and vice versa. In a given
  //   function declaration, all parameters subsequent to a
  //   parameter with a default argument shall have default
  //   arguments supplied in this or previous declarations. A
  //   default argument shall not be redefined by a later
  //   declaration (not even to the same value).
  //
  // C++ [dcl.fct.default]p6:
  //   Except for member functions of class templates, the default arguments
  //   in a member function definition that appears outside of the class
  //   definition are added to the set of default arguments provided by the
  //   member function declaration in the class definition.
  for (unsigned p = 0, NumParams = PrevForDefaultArgs ? PrevForDefaultArgs->getNumParams() : 0; p < NumParams; ++p) {
    // ...
    if (OldParamHasDfl && NewParamHasDfl) {
    // ...
    } else if (OldParamHasDfl) {
    // ...
    } else if (NewParamHasDfl) {
      if (New->getDescribedFunctionTemplate()) {
      // ...
      } else if (New->getTemplateSpecializationKind() != TSK_ImplicitInstantiation && New->getTemplateSpecializationKind() != TSK_Undeclared) {
        // ...
        Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) << (New->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) << New->getDeclName() << NewParam->getDefaultArgRange();

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/p21.cpp

  • clang/test/CXX/temp/temp.spec/temp.expl.spec/p21.cpp:10:22: error: default argument not permitted on an explicit specialization of function 'mf1'
  • clang/test/CXX/temp/temp.spec/temp.expl.spec/p21.cpp:15:27: error: default argument not permitted on an explicit specialization of function 'mf2'