<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/163514>163514</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Clang]Clang rejects `friend auto` function inside class template when a same-named global function exists (accepted by GCC/MSVC/EDG)
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Attacker23
      </td>
    </tr>
</table>

<pre>
     
When a class template defines a `friend auto` function and a global function with the same name exists, Clang rejects the code with a redefinition error, while **GCC, MSVC, and EDG** all accept it: 
  
```  cpp
int g(){return 0;}; 

template<int M>
struct R {
  friend auto g() {
    return M;
  }
};
```   
 
Clang output:  
``` 
<source>:5:15: error: functions that differ only in their return type cannot be overloaded
    5 |   friend auto g() {
      |          ~~~~ ^
<source>:1:5: note: previous definition is here
    1 | int g(){return 0;}; 
      | ~~~ ^
1 error generated.
Compiler returned: 1
```  
  
(See it live: https://godbolt.org/z/E5a1hsfsj)
  
This is strange. Maybe when a `friend` function inside a class template uses an `auto` return type and there exists a global function with the same name, Clang performs conflict checking too early. 
  
Because the following code:  
```cpp
int g() { return 0; }

template<int M>
struct R {
  friend decltype(M) g() {
    return M;
  }
};
```  
is accepted by all compilers, including Clang. https://godbolt.org/z/ecdYTc6vE
  
This suggests that Clang performs name lookup / overloading checks **too early** —  before the return type of the friend `auto` function has been deduced.  
Once deduced, the function should be considered a distinct friend, not an overload of the global `g()`.
  
  

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJykVUGP4jgT_TXmUhqUOCTAgUOgYU6tT_pmtKs9OnYl8bSxI9uhlz30b1-VCTR0Szuz2lYrCJMqv3r1XpUIQXcWccPKLSufZmKMvfObOkYhX9DzYtY4dd4Ay-rfe7QgQBoRAkQ8DkZEBIWtthhAAKuy1mu0CsQYHasyaEcro3YWBB1CZ1wj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