public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/115676] New: gcc allows invalid calling to implicitly-deleted default constructor of a template derived class in template function
@ 2024-06-27  9:14 rush102333 at gmail dot com
  2024-06-27 19:51 ` [Bug c++/115676] [c++11-C++14] gcc allows invalid calling to implicitly-deleted default constructor of a template derived class (non-depdenent) " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: rush102333 at gmail dot com @ 2024-06-27  9:14 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115676
           Summary: gcc allows invalid calling to implicitly-deleted
                    default constructor of a template derived class in
                    template function
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Keywords: accepts-invalid
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rush102333 at gmail dot com
  Target Milestone: ---

The following invalid code is accepted by gcc-13.2.0 and gcc-trunk:

~~~~~~~~~~~~~~~~~~~~~~
struct empty{

};
template <int size>
struct A
: public empty
{
    const int is_valid;
};

template <class T>
void check_a()
{
  const A<0> a {};
}
~~~~~~~~~~~~~~~~~~~~~~

It can be seen that the template class "A" is non-trivial because it has an
empty base class. Thus, the default constructor of "A" should be implicitly
deleted, and the initialization statement "const A<0> a{}" should be invalid. 

But when the initialization happens in a template function, GCC seems to ignore
the error. Changing "check_a" to a non-template function can make the compiler
reject the code by giving the following error message:


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>: In function 'void check_a()':
<source>:14:17: error: use of deleted function 'A<0>::A()'
   14 |   const A<0> a {};
      |                 ^
<source>:5:8: note: 'A<0>::A()' is implicitly deleted because the default
definition would be ill-formed:
    5 | struct A
      |        ^
<source>: At global scope:
<source>:5:8: error: uninitialized const member in 'struct A<0>'
<source>:8:15: note: 'const int A<0>::is_valid' should be initialized
    8 |     const int is_valid;
      |               ^~~~~~~~
<source>: In function 'void check_a()':
<source>:14:17: note: use '-fdiagnostics-all-candidates' to display considered
candidates
   14 |   const A<0> a {};

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Please check https://godbolt.org/z/nGxPdn9jf

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

end of thread, other threads:[~2024-06-28  2:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-27  9:14 [Bug c++/115676] New: gcc allows invalid calling to implicitly-deleted default constructor of a template derived class in template function rush102333 at gmail dot com
2024-06-27 19:51 ` [Bug c++/115676] [c++11-C++14] gcc allows invalid calling to implicitly-deleted default constructor of a template derived class (non-depdenent) " pinskia at gcc dot gnu.org
2024-06-27 19:56 ` [Bug c++/115676] [c++11-C++14] gcc allows invalid calling to implicitly-deleted default constructor of a template derived class (non-dependent) " pinskia at gcc dot gnu.org
2024-06-28  2:02 ` xry111 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).