Clang error: template argument for template type parameter must be a type (err_template_arg_must_be_type)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: template argument for template type parameter must be a type
Type Error
Category Semantic Issue
Internal Id err_template_arg_must_be_type
Internal Message template argument for template type parameter must be a type
Regular Expression (?:error|fatal error)\: template argument for template type parameter must be a type
First Commit 2009-03-14 5a8987ca5113 Update tablegen diagnostic files to be in sync with the def files.

Description

The error is issued by the Clang compiler when a template argument specified for a template type parameter is not a valid type. In C++ template programming, type parameters must be valid types, such as built-in types, user-defined types, or other valid type expressions. This error typically occurs when attempting to use a non-type expression, such as an integer constant, string literal, or other non-type values, in a context where a type is expected for a template type parameter. The compiler checks the provided template argument against the expected template parameter and, if the argument does not qualify as a valid type, this diagnostic message is generated to indicate the mismatch. Identifying and correcting such issues involves ensuring that all template type arguments are indeed types that satisfy the requirements of the template parameter they are intended to instantiate.  
AI Generated

Example

In the following example, a std::vector is attempted to be initialized with an integer constant 5 as its template argument. The syntax std::vector<5> is incorrect because the template parameter for std::vector expects a type that describes the elements to be contained within the vector, such as int or double, rather than an integer constant. Consequently, the Clang compiler issues an error message indicating that a type was expected for the template argument, but instead a non-type value was provided. This error underscores the requirement in C++ that template arguments corresponding to template type parameters must indeed be types.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
#include <vector>

int main() {
  std::vector<5> myVector; // 5 is not a type
}
Compiler Output
<source>:4:15: error: template argument for template type parameter must be a type
/opt/compiler-explorer/gcc-13.2.0/lib/gcc/x86_64-linux-gnu/13.2.0/../../../../include/c++/13.2.0/bits/stl_vector.h:424:21: note: template parameter is declared here


Clang Internals (17.0.6)

Git Commit Message

Update tablegen diagnostic files to be in sync with the def files.

llvm-svn: 67004

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 5284)

bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, TemplateArgumentLoc &AL, SmallVectorImpl<TemplateArgument> &SugaredConverted, SmallVectorImpl<TemplateArgument> &CanonicalConverted) {
  // ...
  default: {
    // ...
    Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;

Triggered in Clang Tests

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

clang/test/SemaCXX/dcl_ambig_res.cpp

  • clang/test/SemaCXX/dcl_ambig_res.cpp:41:4: error: template argument for template type parameter must be a type