public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/96673] New: Friend class with templates and default constructor not recognized in C++14 or later
@ 2020-08-18 10:16 andebjor at gmail dot com
  2020-08-18 16:08 ` [Bug c++/96673] [8/9/10/11 Regression] " mpolacek at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: andebjor at gmail dot com @ 2020-08-18 10:16 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2022-05-13 17:44 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-18 10:16 [Bug c++/96673] New: Friend class with templates and default constructor not recognized in C++14 or later andebjor at gmail dot com
2020-08-18 16:08 ` [Bug c++/96673] [8/9/10/11 Regression] " mpolacek at gcc dot gnu.org
2020-08-18 16:18 ` mpolacek at gcc dot gnu.org
2020-10-12 12:32 ` rguenth at gcc dot gnu.org
2021-04-06 19:00 ` jason at gcc dot gnu.org
2021-04-06 20:42 ` cvs-commit at gcc dot gnu.org
2021-04-06 20:42 ` [Bug c++/96673] [8/9/10 " jason at gcc dot gnu.org
2021-05-14  9:53 ` [Bug c++/96673] [9/10 " jakub at gcc dot gnu.org
2021-05-20 21:35 ` cvs-commit at gcc dot gnu.org
2021-06-01  8:18 ` rguenth at gcc dot gnu.org
2022-05-13 17:41 ` [Bug c++/96673] [9 " cvs-commit at gcc dot gnu.org
2022-05-13 17:44 ` jason at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).