Bug 67054 - Constructor inheritance with non-default constructible members
Summary: Constructor inheritance with non-default constructible members
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 6.1.1
: P3 normal
Target Milestone: 7.2
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
: 80851 (view as bug list)
Depends on:
Blocks:
 
Reported: 2015-07-29 10:37 UTC by witosx
Modified: 2021-08-12 01:55 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 6.1.0, 6.2.0, 6.3.0, 7.1.0, 8.0
Last reconfirmed: 2015-07-29 00:00:00


Attachments
Preprocessed source code (195 bytes, text/plain)
2015-07-29 10:37 UTC, witosx
Details

Note You need to log in before you can comment on or make changes to this bug.
Description witosx 2015-07-29 10:37:28 UTC
Created attachment 36086 [details]
Preprocessed source code

Following code won't compile:

struct NonDefault
{
    NonDefault(int) {}
};

struct Base
{   
    Base(int) {}
};

struct Derived : public Base
{
    NonDefault foo = 4;    
    
    using Base::Base;
};

auto test()
{
    auto d = Derived{ 5 };
}


$ g++ -std=c++14 -c test.cpp
test.cpp: In function ‘auto test()’:
test.cpp:20:25: error: use of deleted function ‘Derived::Derived(int)’
     auto d = Derived{ 5 };
                         ^
test.cpp:15:17: note: ‘Derived::Derived(int)’ is implicitly deleted because the default definition would be ill-formed:
     using Base::Base;
                 ^
test.cpp:15:17: error: no matching function for call to ‘NonDefault::NonDefault()’
test.cpp:3:5: note: candidate: NonDefault::NonDefault(int)
     NonDefault(int) {}
     ^
test.cpp:3:5: note:   candidate expects 1 argument, 0 provided
test.cpp:1:8: note: candidate: constexpr NonDefault::NonDefault(const NonDefault&)
 struct NonDefault
        ^
test.cpp:1:8: note:   candidate expects 1 argument, 0 provided
test.cpp:1:8: note: candidate: constexpr NonDefault::NonDefault(NonDefault&&)
test.cpp:1:8: note:   candidate expects 1 argument, 0 provided


$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /build/gcc-multilib/src/gcc-5.2.0/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --enable-multilib --disable-werror --enable-checking=release --with-default-libstdcxx-abi=gcc4-compatible
Thread model: posix
gcc version 5.2.0 (GCC)
Comment 1 Jonathan Wakely 2015-07-29 15:01:22 UTC
Confirmed.

(The testcase is valid C++11 too, except for the unnecessary auto return type.)
Comment 2 Martin Hierholzer 2016-12-02 14:37:02 UTC
I stumbled across this bug, too (and asked about it on stackoverflow, since I wasn't sure if it's valid C++11, see http://stackoverflow.com/questions/40932844/constructor-inheritance-and-direct-member-initialisation).

This problem is still there in gcc 6.2.0.

The code compiles, if one adds a default constructor to NonDefault, despite that default constructor never gets called in the end. (This can be used as a work-around).
Comment 3 Daniel Boles 2017-05-22 08:15:47 UTC
Still occurs in 4.8 to 7.1, according to this duplicate
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80851

Also, could we perhaps get a more useful title for this one? It describes what is involved in the problem, but not what the problem is.
Comment 4 witosx 2017-05-29 11:05:57 UTC
(In reply to DB from comment #3)
> Still occurs in 4.8 to 7.1, according to this duplicate
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80851
> 
> Also, could we perhaps get a more useful title for this one? It describes
> what is involved in the problem, but not what the problem is.

What title do you suggest?
Comment 5 Jason Merrill 2017-07-26 17:39:57 UTC
Author: jason
Date: Wed Jul 26 17:39:26 2017
New Revision: 250583

URL: https://gcc.gnu.org/viewcvs?rev=250583&root=gcc&view=rev
Log:
        PR c++/67054 - Inherited ctor with non-default-constructible members
        * method.c (walk_field_subobs) Consider member initializers (NSDMIs)
	when deducing an inheriting constructor.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/inh-ctor29.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/method.c
Comment 6 Jason Merrill 2017-08-09 20:18:19 UTC
Author: jason
Date: Wed Aug  9 20:17:47 2017
New Revision: 251000

URL: https://gcc.gnu.org/viewcvs?rev=251000&root=gcc&view=rev
Log:
        PR c++/67054 - Inherited ctor with non-default-constructible members

        * method.c (walk_field_subobs) Consider member initializers (NSDMIs)
	when deducing an inheriting constructor.

Added:
    branches/gcc-7-branch/gcc/testsuite/g++.dg/cpp0x/inh-ctor29.C
Modified:
    branches/gcc-7-branch/gcc/cp/ChangeLog
    branches/gcc-7-branch/gcc/cp/method.c
Comment 7 Aldy Hernandez 2017-09-13 15:47:56 UTC
Author: aldyh
Date: Wed Sep 13 15:47:25 2017
New Revision: 252096

URL: https://gcc.gnu.org/viewcvs?rev=252096&root=gcc&view=rev
Log:
        PR c++/67054 - Inherited ctor with non-default-constructible members
        * method.c (walk_field_subobs) Consider member initializers (NSDMIs)
	when deducing an inheriting constructor.

Added:
    branches/range-gen2/gcc/testsuite/g++.dg/cpp0x/inh-ctor29.C
Modified:
    branches/range-gen2/gcc/cp/ChangeLog
    branches/range-gen2/gcc/cp/method.c
Comment 8 Leonid Koppel 2018-02-12 20:48:02 UTC
Fixed in 7.2.
Comment 9 Jonathan Wakely 2018-02-12 22:40:24 UTC
(In reply to Leonid Koppel from comment #8)
> Fixed in 7.2.

Indeed, thanks.
Comment 10 Andrew Pinski 2021-08-12 01:55:19 UTC
*** Bug 80851 has been marked as a duplicate of this bug. ***