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

            Bug ID: 109923
           Summary: Template friend function defined in a template class
                    becomes a friend of all instantiations, not one
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: egor_suvorov at mail dot ru
  Target Milestone: ---

The following code is accepted by all versions of GCC that I tried (including
"trunk" and 4.1.2 on Godbolt), but rejected by Clang and Visual Studio
(https://godbolt.org/z/z9f9qbPqb):

template<typename T>
struct MyTemplate {
private:
    int x;
public:
    MyTemplate() : x(0) {}
    friend void non_templ_friend(MyTemplate val, MyTemplate<void> weird) {
        // MyTemplate<T> is a friend of non_templ_friend(MyTemplate<T>,
MyTempl<void>)
        val.x++;  // always works
        weird.x++;  // should only work when T=void
    }
};
int main() {
    non_templ_friend(MyTemplate<int>(), MyTemplate<void>());
}

I expect that a new `non_templ_friend` is defined for each `T`. Each
`non_templ_friend` is a friend of the `MyTemplate<T>` it was created within.
Hence, it can only access internals of `MyTemplate<void>` if `T=void`. Hence,
`main()` should not compile: I call `non_templ_friend` created with `T=int`, it
should not be a friend of `MyTemplate<void>`.

Reply via email to