Bug 57082 - brace initialization requires public destructor
Summary: brace initialization requires public destructor
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.7.3
: P3 normal
Target Milestone: 9.3
Assignee: Jason Merrill
URL:
Keywords: rejects-valid
Depends on:
Blocks: 59238
  Show dependency treegraph
 
Reported: 2013-04-26 14:05 UTC by Jonathan Wakely
Modified: 2021-12-04 13:23 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 10.0, 9.2.1
Known to fail: 4.7.3, 4.8.0, 4.9.0, 5.3.0, 6.2.0, 7.5.0, 8.3.0, 9.2.0
Last reconfirmed: 2019-12-10 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Wakely 2013-04-26 14:05:25 UTC
struct X
{
private:
  ~X() {}
};

int main()
{
  new X;    // OK
  new X();  // OK
  new X{};  // ERROR
}

x.cc: In function 'int main()':
x.cc:4:3: error: 'X::~X()' is private
   ~X() {}
   ^
x.cc:11:9: error: within this context
   new X{};
         ^
Comment 1 lucdanton 2016-10-28 05:16:16 UTC
Using a very similar testcase I bisected the issue to r239783:

//----------
struct no_destr {
    no_destr() = default;
protected:
    ~no_destr() = default;
};

int main()
{
    // error: 'no_destr::~no_destr()' is protected within this context
    new no_destr {};
}
Comment 2 Jonathan Wakely 2016-10-28 16:52:47 UTC
Your example failing is a regression in trunk, but my example fails long before r239783 was committed.
Comment 3 Stephane Kaloustian 2016-11-22 10:31:13 UTC
In my understanding, this is related to the creation of a copy. 
Using g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4):

struct B{
    B(){}
    ~B(){}
    B(const B&) = delete;
};

int main (int argc, char** argv)
{
    B myB;
    const B & bref1 = myB; // OK
    const B & bref2(myB);  // OK
    const B & bref3{myB};  // ERROR
    return 0;
}

delref.cpp: In function ‘int main(int, char**)’:
delref.cpp:14:24: error: use of deleted function ‘B::B(const B&)’
     const B & bref3{myB};
                        ^
delref.cpp:5:5: error: declared here
     B(const B&) = delete;
     ^
Comment 4 Jonathan Wakely 2016-11-22 11:35:50 UTC
(In reply to Stephane Kaloustian from comment #3)
> In my understanding, this is related to the creation of a copy. 
> Using g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4):

No, that's a completely different issue, and was fixed for GCC 4.9.0, see Bug 50025.
Comment 5 Jonathan Wakely 2016-11-22 11:43:05 UTC
(In reply to lucdanton from comment #1)
> Using a very similar testcase I bisected the issue to r239783:
> 
> //----------
> struct no_destr {
>     no_destr() = default;
> protected:
>     ~no_destr() = default;
> };
> 
> int main()
> {
>     // error: 'no_destr::~no_destr()' is protected within this context
>     new no_destr {};
> }

And that doesn't depend on brace-init either, so is a different issue. I've reported it as Bug 78469
Comment 6 Jason Merrill 2019-12-11 16:51:41 UTC
Author: jason
Date: Wed Dec 11 16:51:09 2019
New Revision: 279236

URL: https://gcc.gnu.org/viewcvs?rev=279236&root=gcc&view=rev
Log:
	PR c++/57082 - new X{} and private destructor.

build_new_1 already passes tf_no_cleanup to build_value_init, but in this
testcase we end up calling build_value_init by way of
build_special_member_call, so we need to pass it to that function as well.

	* init.c (build_new_1): Also pass tf_no_cleanup to
	build_special_member_call.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/initlist-new2.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/init.c
Comment 7 Jason Merrill 2019-12-13 05:10:46 UTC
Author: jason
Date: Fri Dec 13 05:10:11 2019
New Revision: 279335

URL: https://gcc.gnu.org/viewcvs?rev=279335&root=gcc&view=rev
Log:
	PR c++/57082 - new X{} and private destructor.

build_new_1 already passes tf_no_cleanup to build_value_init, but in this
testcase we end up calling build_value_init by way of
build_special_member_call, so we need to pass it to that function as well.

	* init.c (build_new_1): Also pass tf_no_cleanup to
	build_special_member_call.

Added:
    branches/gcc-9-branch/gcc/testsuite/g++.dg/cpp0x/initlist-new2.C
Modified:
    branches/gcc-9-branch/gcc/cp/ChangeLog
    branches/gcc-9-branch/gcc/cp/init.c
Comment 8 Jason Merrill 2019-12-17 20:15:35 UTC
Fixed for 9.3/10.  Does anyone think it's important to fix this in 8.4?
Comment 9 Andrew Pinski 2021-08-22 01:55:25 UTC
Fixed for GCC 9.3 and GCC 10+.