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{}; ^
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 {}; }
Your example failing is a regression in trunk, but my example fails long before r239783 was committed.
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; ^
(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.
(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
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
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
Fixed for 9.3/10. Does anyone think it's important to fix this in 8.4?
Fixed for GCC 9.3 and GCC 10+.