Clang error: template parameter pack must be the last template parameter (err_template_param_pack_must_be_last_template_parameter)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: template parameter pack must be the last template parameter
Type Error
Category Semantic Issue
Internal Id err_template_param_pack_must_be_last_template_parameter
Internal Message template parameter pack must be the last template parameter
Regular Expression (?:error|fatal error)\: template parameter pack must be the last template parameter
First Commit 2009-06-13 327865db5323 A parameter pack must always come last in a class template.

Description

The error is issued by the Clang compiler when a template declaration does not adhere to the C++ standard requirement that template parameter packs must be positioned as the last parameter in a template list. This rule applies across various contexts including class templates, function templates, and alias templates. A template parameter pack is used to allow a template to accept an arbitrary number of template arguments of a specific kind. Ensuring that the parameter pack is the last template parameter allows for clearer and more predictable template argument deduction and instantiation processes. The presence of this error indicates a violation of this standard, necessitating adjustments to the order of template parameters in the declaration.  
AI Generated

Example

In the following example, a struct named S is declared with two types of template parameters: a template parameter pack Ts and a single type template parameter U. The error message produced by the Clang compiler indicates the violation of the C++ standard rule that a template parameter pack must be positioned as the last parameter in a template parameter list. This compilation error arises because the template parameter pack Ts is declared before the single type template parameter U, contradicting the standard requirement.  
AI Generated


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

[Try out in Compiler Explorer]

Source
template<typename... Ts, typename U> struct S {}; // Parameter pack not last
int main() {}
Compiler Output
<source>:1:22: error: template parameter pack must be the last template parameter


Clang Internals (17.0.6)

Git Commit Message

A parameter pack must always come last in a class template.

llvm-svn: 73269

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

/// Checks the validity of a template parameter list, possibly
/// considering the template parameter list from a previous
/// declaration.
///
/// If an "old" template parameter list is provided, it must be
/// equivalent (per TemplateParameterListsAreEqual) to the "new"
/// template parameter list.
///
/// \param NewParams Template parameter list for a new template
/// declaration. This template parameter list will be updated with any
/// default arguments that are carried through from the previous
/// template parameter list.
///
/// \param OldParams If provided, template parameter list from a
/// previous declaration of the same template. Default template
/// arguments will be merged from the old template parameter list to
/// the new template parameter list.
///
/// \param TPC Describes the context in which we are checking the given
/// template parameter list.
///
/// \param SkipBody If we might have already made a prior merged definition
/// of this template visible, the corresponding body-skipping information.
/// Default argument redefinition is not an error when skipping such a body,
/// because (under the ODR) we can assume the default arguments are the same
/// as the prior merged definition.
///
/// \returns true if an error occurred, false otherwise.
bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, TemplateParameterList *OldParams, TemplateParamListContext TPC, SkipBodyInfo *SkipBody) {
  // ...
  for (TemplateParameterList::iterator NewParam = NewParams->begin(), NewParamEnd = NewParams->end(); NewParam != NewParamEnd; ++NewParam) {
    // ...
    // C++11 [temp.param]p11:
    //   If a template parameter of a primary class template or alias template
    //   is a template parameter pack, it shall be the last template parameter.
    if (SawParameterPack && (NewParam + 1) != NewParamEnd && (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || TPC == TPC_TypeAliasTemplate)) {
      Diag((*NewParam)->getLocation(), diag::err_template_param_pack_must_be_last_template_parameter);

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

Decl *Sema::ActOnConceptDefinition(Scope *S, MultiTemplateParamsArg TemplateParameterLists, IdentifierInfo *Name, SourceLocation NameLoc, Expr *ConstraintExpr) {
  // ...
  // Ensure that the parameter pack, if present, is the last parameter in the
  // template.
  for (TemplateParameterList::const_iterator ParamIt = Params->begin(), ParamEnd = Params->end(); ParamIt != ParamEnd; ++ParamIt) {
    // ...
    if (Param->isParameterPack()) {
      // ...
      Diag(Param->getLocation(), diag::err_template_param_pack_must_be_last_template_parameter);

Triggered in Clang Tests

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

clang/test/CXX/temp/temp.param/p11-0x.cpp

  • clang/test/CXX/temp/temp.param/p11-0x.cpp:26:22: error: template parameter pack must be the last template parameter
  • clang/test/CXX/temp/temp.param/p11-0x.cpp:30:22: error: template parameter pack must be the last template parameter
  • clang/test/CXX/temp/temp.param/p11-0x.cpp:34:17: error: template parameter pack must be the last template parameter
  • clang/test/CXX/temp/temp.param/p11-0x.cpp:37:17: error: template parameter pack must be the last template parameter
  • clang/test/CXX/temp/temp.param/p11-0x.cpp:41:38: error: template parameter pack must be the last template parameter
  • clang/test/CXX/temp/temp.param/p11-0x.cpp:44:38: error: template parameter pack must be the last template parameter