Clang error: deduction guide must be declared in the same scope as template A (err_deduction_guide_wrong_scope)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: deduction guide must be declared in the same scope as template A (since 5.0)
Type Error
Category Semantic Issue (since 5.0)
Internal Id err_deduction_guide_wrong_scope (since 5.0)
Internal Message deduction guide must be declared in the same scope as template %q0 (since 5.0)
Regular Expression (?:error|fatal error)\: deduction guide must be declared in the same scope as template (.*?)
First Commit 2017-02-10 278890f85bba [c++1z] Enforce restriction that deduction guide is declared in the same scope as its template.

Description

The error is issued by the Clang compiler when a deduction guide is declared outside of the scope where its corresponding template is declared. In C++, a deduction guide suggests how template argument deduction should be performed for a template class, requiring both the template and its deduction guide to reside in the same scope. This rule ensures clarity in template instantiation and prevents ambiguity that could arise if deduction guides were allowed in different scopes. When Clang encounters a deduction guide that does not comply with this rule, it generates this error message to alert the developer of the misplacement. The enforcement of this rule is important for maintaining the consistency and predictability of template deduction across different compiler implementations.  
AI Generated

Example

In the following example, a template class S is defined with a constructor that accepts a single argument of any type T. Following the definition, a deduction guide for the template class S is appropriately placed in the global scope, aligning with the scope of the template class itself. However, another deduction guide for the template S is incorrectly declared within the namespace N. This deduction guide aims to specify how the constructor's arguments should deduce the template parameters for the class template S. The Clang compiler identifies this placement as erroneous because the deduction guide does not reside in the same scope as its associated template class S. Consequently, an error message is generated, highlighting that the deduction guide must be located in the same scope as the template class to which it applies. This example illustrates the requirement for deduction guides to be in the same scope as their corresponding template class declarations, as enforced by the Clang compiler.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
// Deduction guide outside the namespace of its template

template <typename T> struct S { S(T); };

// Deduction guide in global scope
template <typename T> S(T) -> S<T>;

namespace N {
    // Invalid: Deduction guide in different scope
    template <typename T> S(T) -> S<T>;
}
Compiler Output
<source>:10:27: error: deduction guide must be declared in the same scope as template 'S'
<source>:3:30: note: template is declared here


Clang Internals (17.0.6)

Git Commit Message

[c++1z] Enforce restriction that deduction guide is declared in the same scope as its template.

llvm-svn: 294778

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

/// Check the validity of a declarator that we parsed for a deduction-guide.
/// These aren't actually declarators in the grammar, so we need to check that
/// the user didn't specify any pieces that are not part of the deduction-guide
/// grammar. Return true on invalid deduction-guide.
bool Sema::CheckDeductionGuideDeclarator(Declarator &D, QualType &R, StorageClass &SC) {
  // ...
  // C++ [temp.deduct.guide]p3:
  //   A deduction-gide shall be declared in the same scope as the
  //   corresponding class template.
  if (!CurContext->getRedeclContext()->Equals(GuidedTemplateDecl->getDeclContext()->getRedeclContext())) {
    Diag(D.getIdentifierLoc(), diag::err_deduction_guide_wrong_scope) << GuidedTemplateDecl;

Triggered in Clang Tests

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

clang/test/CXX/temp/temp.deduct.guide/p3.cpp

  • clang/test/CXX/temp/temp.deduct.guide/p3.cpp:46:3: error: deduction guide must be declared in the same scope as template 'WrongScope::(anonymous namespace)::AnonNS1'
  • clang/test/CXX/temp/temp.deduct.guide/p3.cpp:50:5: error: deduction guide must be declared in the same scope as template 'WrongScope::AnonNS2'
  • clang/test/CXX/temp/temp.deduct.guide/p3.cpp:57:3: error: deduction guide must be declared in the same scope as template 'WrongScope::N::NamedNS1'
  • clang/test/CXX/temp/temp.deduct.guide/p3.cpp:60:3: error: deduction guide must be declared in the same scope as template 'WrongScope::N::NamedNS2'
  • clang/test/CXX/temp/temp.deduct.guide/p3.cpp:65:5: error: deduction guide must be declared in the same scope as template 'WrongScope::ClassMemberA::X'