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

* [Bug c++/96673] [8/9/10/11 Regression] Friend class with templates and default constructor not recognized in C++14 or later
  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 ` mpolacek at gcc dot gnu.org
  2020-08-18 16:18 ` mpolacek at gcc dot gnu.org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-08-18 16:08 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
                 CC|                            |mpolacek at gcc dot gnu.org
   Target Milestone|---                         |8.5
   Last reconfirmed|                            |2020-08-18
           Keywords|                            |rejects-valid
            Summary|Friend class with templates |[8/9/10/11 Regression]
                   |and default constructor not |Friend class with templates
                   |recognized in C++14 or      |and default constructor not
                   |later                       |recognized in C++14 or
                   |                            |later

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Confirmed.  GCC 5 actualled ICEd on this.  GCC 4.9 compiled it even with
-std=c++14.

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

* [Bug c++/96673] [8/9/10/11 Regression] Friend class with templates and default constructor not recognized in C++14 or later
  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
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-08-18 16:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
The ICE with -std=c++14 started with r216750.  It was fixed by r234442, but the
error appeared.

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

* [Bug c++/96673] [8/9/10/11 Regression] Friend class with templates and default constructor not recognized in C++14 or later
  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
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-10-12 12:32 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2

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

* [Bug c++/96673] [8/9/10/11 Regression] Friend class with templates and default constructor not recognized in C++14 or later
  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
                   ` (2 preceding siblings ...)
  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
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jason at gcc dot gnu.org @ 2021-04-06 19:00 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at gcc dot gnu.org
           Assignee|unassigned at gcc dot gnu.org      |jason at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

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

* [Bug c++/96673] [8/9/10/11 Regression] Friend class with templates and default constructor not recognized in C++14 or later
  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
                   ` (3 preceding siblings ...)
  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
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-04-06 20:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:de03b82f3ca9103eba3699d1dc91b1d0ee1f16cb

commit r11-8018-gde03b82f3ca9103eba3699d1dc91b1d0ee1f16cb
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Apr 6 15:13:02 2021 -0400

    c++: access checking in aggregate initialization [PR96673]

    We were deferring access checks while parsing B<int>{}, didn't adjust that
    when we went to instantiate the default member initializer for B::c,
    deferred access checking for C::C, and then checked it after parsing
    B<int>{}, back in the main() context which has no access.  We need to do
the
    access checks in the class context of the DMI.

    I tried fixing this in push_to/pop_from_top_level, but that caused several
    regressions.

    gcc/cp/ChangeLog:

            PR c++/96673
            * init.c (get_nsdmi): Don't defer access checking.

    gcc/testsuite/ChangeLog:

            PR c++/96673
            * g++.dg/cpp1y/nsdmi-aggr13.C: New test.

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

* [Bug c++/96673] [8/9/10 Regression] Friend class with templates and default constructor not recognized in C++14 or later
  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
                   ` (4 preceding siblings ...)
  2021-04-06 20:42 ` cvs-commit at gcc dot gnu.org
@ 2021-04-06 20:42 ` jason at gcc dot gnu.org
  2021-05-14  9:53 ` [Bug c++/96673] [9/10 " jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jason at gcc dot gnu.org @ 2021-04-06 20:42 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to work|                            |11.0
            Summary|[8/9/10/11 Regression]      |[8/9/10 Regression] Friend
                   |Friend class with templates |class with templates and
                   |and default constructor not |default constructor not
                   |recognized in C++14 or      |recognized in C++14 or
                   |later                       |later

--- Comment #4 from Jason Merrill <jason at gcc dot gnu.org> ---
Fixed for 11 so far.

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

* [Bug c++/96673] [9/10 Regression] Friend class with templates and default constructor not recognized in C++14 or later
  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
                   ` (5 preceding siblings ...)
  2021-04-06 20:42 ` [Bug c++/96673] [8/9/10 " jason at gcc dot gnu.org
@ 2021-05-14  9:53 ` jakub at gcc dot gnu.org
  2021-05-20 21:35 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-05-14  9:53 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|8.5                         |9.4

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
GCC 8 branch is being closed.

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

* [Bug c++/96673] [9/10 Regression] Friend class with templates and default constructor not recognized in C++14 or later
  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
                   ` (6 preceding siblings ...)
  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
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-05-20 21:35 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Jason Merrill
<jason@gcc.gnu.org>:

https://gcc.gnu.org/g:e206f71eaf98149224269b00407bc5def6ae2dca

commit r10-9848-ge206f71eaf98149224269b00407bc5def6ae2dca
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Apr 6 15:13:02 2021 -0400

    c++: access checking in aggregate initialization [PR96673]

    We were deferring access checks while parsing B<int>{}, didn't adjust that
    when we went to instantiate the default member initializer for B::c,
    deferred access checking for C::C, and then checked it after parsing
    B<int>{}, back in the main() context which has no access.  We need to do
the
    access checks in the class context of the DMI.

    I tried fixing this in push_to/pop_from_top_level, but that caused several
    regressions.

    gcc/cp/ChangeLog:

            PR c++/96673
            * init.c (get_nsdmi): Don't defer access checking.

    gcc/testsuite/ChangeLog:

            PR c++/96673
            * g++.dg/cpp1y/nsdmi-aggr13.C: New test.

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

* [Bug c++/96673] [9/10 Regression] Friend class with templates and default constructor not recognized in C++14 or later
  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
                   ` (7 preceding siblings ...)
  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
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-06-01  8:18 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|9.4                         |9.5

--- Comment #7 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 9.4 is being released, retargeting bugs to GCC 9.5.

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

* [Bug c++/96673] [9 Regression] Friend class with templates and default constructor not recognized in C++14 or later
  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
                   ` (8 preceding siblings ...)
  2021-06-01  8:18 ` rguenth at gcc dot gnu.org
@ 2022-05-13 17:41 ` cvs-commit at gcc dot gnu.org
  2022-05-13 17:44 ` jason at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-05-13 17:41 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[9/10 Regression] Friend    |[9 Regression] Friend class
                   |class with templates and    |with templates and default
                   |default constructor not     |constructor not recognized
                   |recognized in C++14 or      |in C++14 or later
                   |later                       |

--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-9 branch has been updated by Jason Merrill
<jason@gcc.gnu.org>:

https://gcc.gnu.org/g:e0cc2713a4076edd4f20b80b0fe217c9ab1676b2

commit r9-10164-ge0cc2713a4076edd4f20b80b0fe217c9ab1676b2
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Apr 6 15:13:02 2021 -0400

    c++: access checking in aggregate initialization [PR96673]

    We were deferring access checks while parsing B<int>{}, didn't adjust that
    when we went to instantiate the default member initializer for B::c,
    deferred access checking for C::C, and then checked it after parsing
    B<int>{}, back in the main() context which has no access.  We need to do
the
    access checks in the class context of the DMI.

    I tried fixing this in push_to/pop_from_top_level, but that caused several
    regressions.

    gcc/cp/ChangeLog:

            PR c++/96673
            * init.c (get_nsdmi): Don't defer access checking.

    gcc/testsuite/ChangeLog:

            PR c++/96673
            * g++.dg/cpp1y/nsdmi-aggr13.C: New test.

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

* [Bug c++/96673] [9 Regression] Friend class with templates and default constructor not recognized in C++14 or later
  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
                   ` (9 preceding siblings ...)
  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
  10 siblings, 0 replies; 12+ messages in thread
From: jason at gcc dot gnu.org @ 2022-05-13 17:44 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #9 from Jason Merrill <jason at gcc dot gnu.org> ---
Fixed for 9.5/10.4/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).