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

            Bug ID: 96673
           Summary: Friend class with templates and default constructor
                    not recognized in C++14 or later
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andebjor at gmail dot com
  Target Milestone: ---

This code fails when compiled as C++14 or C++17 with the error message "error:
'C<T>::C(A<T>&) [with T = int]' is private within this context".

---8<---
template <class T>
class A {};

template <class T>
class B;

template <class T>
class C {
    private:

    friend class B<T>;

    explicit C(A<T>&) {};
};


template <class T>
class B {
    public:
    B() = default;
    //B() {};       // << This implementation of the constructor makes it work

    A<T> a = {};
    C<T> c = C<T>{a};
};

int main() {
    auto b = B<int>{};
    auto &c = b.c;
}
--->8---

Compiler explorer link: https://godbolt.org/z/4ofMdM

This code builds with `clang` but fails with most versions of GCC.
* Version 4.9 works
* Version 5.x: internal compiler error
* Version 6.x up until trunk version: "C<T>::C(A<T>&) [with T = int]' is
private within this context"

There are several ways to make the code build:

* Removing templating. Without templates the error about the constructor being
private does not appear.
* Changing the constructor of `B` from `B() = default;` to `B() {};`
* Building with C++11.

Reply via email to