Clang error: use of ... B requires template arguments (err_template_missing_args)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: use of class template A requires template arguments
Type Error
Category Semantic Issue
Internal Id err_template_missing_args
Internal Message use of class template %0 requires template arguments
Regular Expression (?:error|fatal error)\: use of (?:class template|function template|variable template|alias template|template template parameter|concept|template) (.*?) requires template arguments
First Commit 2010-04-08 823015d627b8 When a template (without arguments) is passed as a template type

Description

The error is issued by the Clang compiler when there is an attempt to use a template—be it a class, function, variable, alias, template template parameter, concept, or an unnamed template—without specifying its required template arguments. Templates in C++ are a powerful feature that allows for the type and function parameterization, providing the ability to use types or values as parameters in classes and functions. When a template is declared, it expects certain arguments to be provided in order to correctly generate or instantiate the type, function, or variable from the template.

This specific diagnostic is triggered when the compiler encounters a template usage that lacks the necessary template arguments. The absence of these arguments means the compiler cannot instantiate the template, leading to this error. Each template has its own set of requirements in terms of the number and type of arguments it expects, and these need to be provided for the template to be used effectively.

It's important to distinguish between the definition of a template and its instantiation. The error specifically concerns the latter, where the template is being put to use, but without the required parameters. Correcting this error usually involves reviewing the template's declaration to determine what arguments are expected and then supplying them wherever the template is used. This ensures that the compiler can correctly complete the instantiation process, thereby resolving the semantic issue pointed out by the error.

 
AI Generated

Example

In the following example, a class template named A is defined that takes a type parameter T and attempts to inherit from it. This illustrates the flexibility of templates in C++, allowing classes to be parameterized with types. However, when trying to create an instance of A named a, the code incorrectly omits providing any template arguments for A. This leads to a compiler error, as the instantiation of a attempts to use the class template A without specifying the necessary type argument. The compiler's output precisely indicates the issue, pointing out the line where the error occurs and emphasizing that A requires template arguments, along with highlighting the declaration of A to assist in correcting the mistake.  
AI Generated


Flags -xc++ -std=c++11

[Try out in Compiler Explorer]

Source
template<typename T> class A : T {}; A<A> a; // A used without template arguments
Compiler Output
<source>:1:40: error: use of class template 'A' requires template arguments
<source>:1:28: note: template is declared here


Clang Internals (17.0.6)

Git Commit Message

When a template (without arguments) is passed as a template type
parameter, explicitly ask the user to give it arguments.  We used to
complain that it wasn't a type and expect the user to figure it out.

llvm-svn: 100729

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

void Sema::diagnoseMissingTemplateArguments(TemplateName Name, SourceLocation Loc) {
  Diag(Loc, diag::err_template_missing_args) << (int)getTemplateNameKindForDiagnostics(Name) << Name;

Triggered in Clang Tests

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

clang/test/Parser/cxx2a-constrained-template-param.cpp

  • clang/test/Parser/cxx2a-constrained-template-param.cpp:49:27: error: use of class template 'test1' requires template arguments