<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/77104>77104</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Clang] Can not friend a function in root namespace with non-void return type
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Sh3Rm4n
</td>
</tr>
</table>
<pre>
Compiler Explorer: https://godbolt.org/z/Gzhdo54W7
Clang is not able to friend a function, which lives in the root namespace.
```cpp
#include <cstdint>
uint32_t friend_foo();
namespace n {
class bar {
friend uint32_t ::friend_foo();
void private_method() {}
};
}
uint32_t friend_foo() {
n::bar b;
b.private_method();
return 0;
}
```
It has problems to swallow the `friend uint32_t ::friend_foo();` syntax.
```
<source>:7:12: error: 'uint32_t' (aka 'unsigned int') is not a class, namespace, or enumeration
7 | friend uint32_t ::friend_foo();
|
```
Switching the return type to `void` removes the error.
```diff
3c3
< uint32_t friend_foo();
---
> void friend_foo();
7c7
< friend uint32_t ::friend_foo();
---
> friend void ::friend_foo();
14c14
< uint32_t friend_foo() {
---
> void friend_foo() {
18,19d17
<
< return 0;
```
or moving the class and the function to the same namespace, so that the `::` prefix is not needed does remove the error.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVU2P4ygQ_TX4UkpkwA7OwYdMMlntdeewxxY2OGYXgwU46Z5fvwLH-djpnh6NFMUCF1TVe6_8uPfqZKSsUfkFlYeMT6G3rv7W07-GwmSNFW_13g6j0tLB19dRWycdojvoQxg9ojtEjogcT1Y0Voe1dSdEjt8ROf7xvRe2LP5mKD-gfDf_7zU3J1AejA3AGy0hWOickkYAh24ybVDWILKHS6_aHrQ6Sw_KQOglOGsDGD5IP_JWruHxYrTJ5187jtcdQpVp9SQkILpvfRDKBES_Pp6alAmUvIRrCS-dtYhUiGwR_fIYd0sKBhC7vmo19x4a7u5bALB0c7s6QkR3HyaIR85WCRidOvMgXwYZeivmqHQzO1xLYYd7WbfNn_fxXJqZa4klN_cCmvV7qZ8KdDJMzkD-Tv4F98dy_gzQcw-js42Wg48c-wvX2l4Sj2iT_zpImxz8mwn89QO-r0u693ZyrYwE0x1DdIdJVKl0zia5IsKWbIgwQKTi__K0a9IACIjyICyCtugTEsVRjTcBxIV1IM00SMeTWG8oMUBsD7-pAbge_gms3y4qtL0yp3kaZk7C25iGCG3yKKOIl5ODjWMTo1L_H0AnVNfNW7SlNxzh86FYrVZL-NdZvB-Gspbdb_4NaJ5SPZxPWT85i4sWF7_Q131IPm_sHosrRPZ4K_BDg8-t_jg177FqHQz2vLA6f1S4EWm1fBAjwXHt-SCftejjCx6WuZoRiSIYnezU6yJlI6WQAoSV_iqPB3VkoqZiS7c8kzVmeVFsGGY062tO5JZ1mLNtXvGya0pcYdoRXHHWsLLkmapJTooc5yUu8w2ma8JwRUtMtlVZtNuqQkUuB670WuvzEM0hU95PsmYM50WmeSO1T85DSButARESTcjVMX7VTCePilwrH_z9hqCCTnaVzASVB9hzk7r8wUiiczy7BlxU6MFYs0r0PgxRNjld_8_UVOinZt3aAZFjzH99rEZn_5FtQOSY2vGIHFNH_wUAAP__vt8fBw">