Clang error: declaration of A shadows template parameter (err_template_param_shadow)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: declaration of A shadows template parameter
Type Error
Category Semantic Issue
Internal Id err_template_param_shadow
Internal Message declaration of %0 shadows template parameter
Regular Expression (?:error|fatal error)\: declaration of (.*?) shadows template parameter
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 local variable, parameter, or other declaration within a template function or class has the same name as one of the template parameters. This situation is problematic because the inner declaration "shadows" the template parameter, making it inaccessible within the scope where the shadowing occurs. Consequently, this can lead to confusion and errors in code where the template parameter's value is expected to be used rather than the local declaration.

In C++, shadowing refers to the practice of defining a new entity in a scope that has the same name as an entity in an outer scope. While sometimes useful, shadowing can introduce subtle bugs when the program's behavior depends on which entity is referenced by a given name. In the context of templates, shadowing a template parameter with a local declaration can obscure the template's intent and make the code harder to understand and maintain.

 
AI Generated

Example

In the following example, a template function named f is defined with a single template parameter T. Inside the function, an integer variable is also declared with the name T. This local declaration of T as an integer variable shadows the template parameter T. Consequently, within the scope of the function f, any reference to T will refer to the integer variable, not to the template parameter. This shadowing leads to the compiler issuing an error message, as it can result in confusion or unintended behavior in the program if the template parameter was intended to be used within the function.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
template<typename T>
void f() {
  int T = 0; // T shadows
}
Compiler Output
<source>:3:7: error: declaration of 'T' shadows template parameter
<source>:1:19: 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/SemaLambda.cpp (line 1380)

void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro, Declarator &ParamInfo, const DeclSpec &DS) {
  // ...
  if (TemplateParams) {
    for (const auto *TP : TemplateParams->asArray()) {
      // ...
      for (const auto &Capture : Intro.Captures) {
        if (Capture.Id == TP->getIdentifier()) {
          Diag(Capture.Loc, diag::err_template_param_shadow) << Capture.Id;

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

/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
/// that the template parameter 'PrevDecl' is being shadowed by a new
/// declaration at location Loc. Returns true to indicate that this is
/// an error, and false otherwise.
void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
  // ...
  unsigned DiagId = getLangOpts().MSVCCompat ? diag::ext_template_param_shadow : diag::err_template_param_shadow;

Triggered in Clang Tests

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

clang/test/CXX/dcl.dcl/dcl.spec/dcl.typedef/p2-0x.cpp

  • clang/test/CXX/dcl.dcl/dcl.spec/dcl.typedef/p2-0x.cpp:80:11: error: declaration of 'T' shadows template parameter
  • clang/test/CXX/dcl.dcl/dcl.spec/dcl.typedef/p2-0x.cpp:98:11: error: declaration of 'T' shadows template parameter