[Bug c++/102995] Template friend class declaration of same class with different template parameters fails to allow private methods access

pinskia at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Sun Oct 31 02:22:33 GMT 2021


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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #2)
> I suspect the original testcase is invalid code and clang is incorrect in
> accepting it.

You can limit the friendship to only the operator== that have the same second
template operand if you need to limit the friendship slightly more:
template<typename A, typename B> class First {
  public:
    First() = default;

  private:
    int GetId() const {
      return 1;
    }
    template<typename U, typename D>
    friend bool operator==(const First<U, B>& lhs,
                           const First<D, B>& rhs);
};
template<typename U, typename D, typename V>
bool operator==(const First<U, V>& lhs,
                const First<D, V>& rhs) {
                             return lhs.GetId() == rhs.GetId();
                           }
int main() {
  First<int, bool> a;
  First<void *, bool> b;

  return a == b;
}


More information about the Gcc-bugs mailing list