Clang error: no matching function found in local scope (err_no_matching_local_friend)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: no matching function found in local scope
Type Error
Category Semantic Issue
Internal Id err_no_matching_local_friend
Internal Message no matching function found in local scope
Regular Expression (?:error|fatal error)\: no matching function found in local scope
First Commit 2010-10-13 f7cfb2212c65 Support friend function declarations in local classes correctly.

Description

The error is issued by the Clang compiler when it encounters a situation where a function call is made, but no declaration of the function can be found within the local scope. This can occur in several contexts, such as when attempting to use a friend function within a local class, but the function does not exist or is not accessible in the given scope. Local classes are classes defined within a function, and the C++ standard has specific rules regarding their usage, including restrictions on friend functions.

Clang enforces these rules strictly and provides detailed error messages to help identify the issue. The mentioned error typically suggests a mismatch between the function's usage and its available declarations, potentially requiring the programmer to either define the function within the appropriate scope or include a declaration that makes the function accessible in the local context.

Friend functions in local classes are a particular case where the standard’s restrictions come into play, and this error message might be triggered if the compiler can't resolve a friend function declaration inside a local class. This situation mandates careful consideration of the scope and declaration of functions intended for use as friends of local classes, to ensure they are correctly recognized and accessible by the compiler.

 
AI Generated

Example

In the following example, a class named C is defined within the main function, making it a local class. Within this local class, an attempt is made to declare a friend function f using a friend declaration. However, the function is not only declared but also defined within the class body, which is not permitted for local classes according to C++ standards. As a result, the Clang compiler emits an error message, indicating it cannot find a matching function in the local scope that could correspond to the friend declaration inside the local class. This scenario highlights a common mistake when dealing with friend functions in local classes, underscoring that friend functions must be declared but not defined within the class body.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
int main() {
    class C {
        friend void f() {}
        // Friend function defined inside local class
    };
    return 0;
}
Compiler Output
<source>:3:21: error: friend function cannot be defined in a local class
<source>:3:21: error: no matching function found in local scope


Clang Internals (17.0.6)

Git Commit Message

Support friend function declarations in local classes correctly.
Fixes a crash and diagnoses the error condition of an unqualified
friend which doesn't resolve to something.  I'm still not certain how
this is useful.

llvm-svn: 116393

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/SemaDecl.cpp (line 8955)

/// Generate diagnostics for an invalid function redeclaration.
///
/// This routine handles generating the diagnostic messages for an invalid
/// function redeclaration, including finding possible similar declarations
/// or performing typo correction if there are no previous declarations with
/// the same name.
///
/// Returns a NamedDecl iff typo correction was performed and substituting in
/// the new declaration name does not cause new errors.
static NamedDecl *DiagnoseInvalidRedeclaration(Sema &SemaRef, LookupResult &Previous, FunctionDecl *NewFD, ActOnFDArgs &ExtraArgs, bool IsLocalFriend, Scope *S) {
  // ...
  unsigned DiagMsg = IsLocalFriend ? diag::err_no_matching_local_friend : NewFD->getFriendObjectKind() ? diag::err_qualified_friend_no_match : diag::err_member_decl_does_not_match;

Triggered in Clang Tests

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

clang/test/CXX/class.access/class.friend/p11.cpp

  • clang/test/CXX/class.access/class.friend/p11.cpp:16:19: error: no matching function found in local scope
  • clang/test/CXX/class.access/class.friend/p11.cpp:36:21: error: no matching function found in local scope
  • clang/test/CXX/class.access/class.friend/p11.cpp:89:23: error: no matching function found in local scope