https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122067
Bug ID: 122067
Summary: misleading diagnostic message when referring a closure
member in a friend declaration (C++14 or above)
Product: gcc
Version: 15.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: ing.russomauro at gmail dot com
Target Milestone: ---
>From C++ standards (since C++14 up to at least C++23):
"A member of a closure type shall not be explicitly instantiated, explicitly
specialized, or named in a friend declaration."
Indeed, gcc 15.2 rejects the following code (flag at least C++14):
See: https://godbolt.org/z/zxoz7rxjG
auto f = []{};
#if __cplusplus >= 201703L
#define CE_17 constexpr
#else
#define CE_17
#endif
class S{
friend CE_17 auto decltype(f)::operator()() const; // rejected line
void func(){
auto (decltype(f)::*p)() const = &decltype(f)::operator();
(f.*p)();
}
};
But the diagnostic message is:
<source>:10:23: error: no declaration matches '<lambda()>'
10 | friend CE_17 auto decltype(f)::operator()() const;
| ^~~~~~~~~~~
<source>:1:10: note: candidate is: '<lambda()>'
1 | auto f = []{};
| ^
<source>:1:11: note: 'struct<lambda()>' defined here
1 | auto f = []{};
Not clear to me whether the error message reveals some other ill-form in the
code, or eventually a bug for gcc, or this is just matter of a misleading
message not well focusing on the need to reject referring closure members in a
friend declaration.
Note I have added the func() method in the class S just to show that everything
works correctly there.