Is the following snippet of code well-formed?
struct A { ~A() = delete; };
A *pa{new A{}};
class B { ~B() = default; };
B *pb{new B{}};
At a first glace it would seems as if the deleted dtor of A and the private explicitly-defaulted dtor of B are never used (intentional memory leak, if you will), which would arguably imply that it is well-formed.
Clang accepts the program for various compiler and C++ versions (C++11 through C++2a).
GCC, on the other hand, rejects the program for various compiler and C++ versions.
struct A { ~A() = delete; }; A *pa{new A{}}; // GCC error: use of deleted function 'A::~A()' class B { ~B() = default; }; B *pb{new B{}}; // GCC error: 'B::~B()' is private within this context
(It if well-formed; before I file a bug report: Is there any open GCC bug report for this corner case? I've searched GCC:s bugzilla myself to no avail.)
Peculiarly GCC accepts the case of a private user-provided destructor:
class C { ~C(); };
C *pc{new C{}}; // OK
class D { ~D() {} };
D *pd{new D{}}; // OK
which could hint for some GCC doing something special for aggregate classes, as A and B are aggregates whereas C and D are not. However GCC is no consistent with this behaviour, as the following example
struct E {
~E() = delete;
private:
int x;
};
E *pe{new E{}};
where E is not an aggregate (private data member) is likewise rejected like the for the aggregate classes A and B above, whereas the examples of F and G below (an aggregate until C++20, and a non-aggregate, respectively)
struct F {
F() = default;
~F() = delete;
};
F *pf{new F{}};
struct G {
G() = default;
~G() = delete;
private:
int x;
};
G *pg{new G{}};
are both accepted by GCC.