https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118255

            Bug ID: 118255
           Summary: Unnecessary error on variable shadowing for friend
                    declaration inside template class with non-type
                    parameter
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wangbopku15 at gmail dot com
  Target Milestone: ---

Consider:

$g++ -Wshadow tst_1.cpp

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

template <int non_template>
struct S {
    friend class non_template;
};

class non_template{};
S<0> s;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

GCC rejects it by complaining that the class name in the friend declaration
shadows the template parameter, but this is unreasonable. The name lookup
mechanism should have found the global declaration of 'class non_template{};'
here, and the non-type template parameter, which is an integer type, should not
be considered in the case of friend class declaration.

MSVC, clang, and EDG accept it.

The diagnostic of GCC:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<source>:4:18: error: declaration of 'struct non_template' shadows template
parameter [-Wtemplate-body]
    4 |     friend class non_template;
      |                  ^~~~~~~~~~~~
<source>:2:11: note: template parameter 'non_template' declared here
    2 | template <int non_template>
      |           ^~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Please see https://godbolt.org/z/sahc3fsxE

It is also worth noting that the code can compile if the 'class non_template'
is declared before the friend declaration:

https://godbolt.org/z/4T9Eahcod

Reply via email to