https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115915
Bug ID: 115915 Summary: gcc fails to detect invalid friend declaration of classes in different namespaces Product: gcc Version: 13.2.0 Status: UNCONFIRMED Keywords: accepts-invalid Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: rush102333 at gmail dot com Target Milestone: --- Please take a look at the following code: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ namespace std_1 { template<class> struct extent{}; } using namespace std_1; struct S { friend class extent; }; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The statement "friend class extent" in line 9 is syntactically invalid. Because "extent" is a template class, the template argument value of this class must be specified when it is declared as a friend class of other classes. For example, "friend class extent<int>". However, GCC seems to ignore that rule and accept the code. This only happens when "extent" is declared in an independent namespace. Clang rejects that code by giving the following message: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <source>:9:23: error: template 'extent' cannot be referenced with the 'class' specifier 9 | friend class extent;}; | ^ <source>:2:28: note: declared here 2 | template<class> struct extent; | ^ 1 error generated. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MSVC also rejects the code, but EDG and ICC seem to accept it. https://godbolt.org/z/xMbvaf768 Without namespace: https://godbolt.org/z/evdEWaqT7