4

I come across the rule (section N3797::12.8/11 [class.copy])

An implicitly-declared copy/move constructor is an inline public member of its class. A defaulted copy/ move constructor for a class X is defined as deleted (8.4.3) if X has:

[...]

— any direct or virtual base class or non-static data member of a type with a destructor that is deleted or inaccessible from the defaulted constructor, or

[...]

But I can't get the point of deleted destructor appearing in a virtual or direct base class at all. Consider the following simple example:

struct A
{
    ~A() = delete;
    A(){ }
};

struct B : A
{
    B(){ }; //error: use of deleted function 'A::~A()'
};

B b; 

int main() { }

DEMO

It's perfectly unclear to me. I defined 0-argument constructor explcitly and it doesn't use base class destructor. But compiler thinks otherwise. It won't work even if we define B's destructor explicitly:

struct A
{
    ~A() = delete;
    A(){ }
};

struct B : A
{
    B(){ };
    ~B(){ };
};

//B b;

int main() {
}

DEMO

Couldn't you clarify that thing?

  • Related: [How does =delete on destructor prevents allocation?](http://stackoverflow.com/questions/18847739/how-does-delete-on-destructor-prevents-allocation) – Nawaz Nov 15 '14 at 05:43
  • @RaphaelM. Yes, variables with static storage duration will be destroyed after returning from main function. It's clear. So what's the point of such destructors? –  Nov 15 '14 at 05:43
  • @Nawaz But in my second example I don't ever allocate a variable. –  Nov 15 '14 at 05:45
  • @DmitryFucintv You don't allocate, what about not deriving A in B. You end up with just A, which can't be used in static allocations, something that may be viewed as a feature. Also, not being able to derive can be viewed as a feature, but for that there's `final` now. – oblitum Nov 15 '14 at 05:50
  • @pepper_chico Couldn't you provide a reference preventing that allocation? –  Nov 15 '14 at 05:52
  • @DmitryFucintv http://bl.ocks.org/anonymous/raw/b35134df97e00a135449/ – oblitum Nov 15 '14 at 05:55
  • @pepper_chico I can't understand what's that got to do with my question. I haven't decalred a variable in the second example, just declared A as a superclass for B. –  Nov 15 '14 at 05:58
  • @DmitryFucintv it ilustrates what I've said, I can enforce the class not have instances being created, while allowing its static member functions to be used. That's just one point for making it useful. – oblitum Nov 15 '14 at 06:02
  • @DmitryFucintv You may try to be more precise on what you're asking, I'm still unsure whether you're looking for a defect in the compiler not following wording of the standard somehow regarding deleted-destructors, or whether you want to know whether they have any point or usability. – oblitum Nov 15 '14 at 06:05
  • @DmitryFucintv ah, now I got what you're looking for. Well, I dunno all the wording standard, but through your wording, to me at last the explicit constructor of a derived class should be fine to have... since it doesn't needs A's destructor, only B's destructor would need ther other one, afaik. – oblitum Nov 15 '14 at 06:16
  • @DmitryFucintv What about: http://bl.ocks.org/anonymous/raw/0d95491c1500023866ab/ – oblitum Nov 15 '14 at 06:22

1 Answers1

1

The rationale for that bullet is covered in defect report 1191: Deleted subobject destructors and implicitly-defined constructors which says:

Consider the following example:

struct A {
   A();
   ~A() = delete;
};

struct B: A { };
B* b = new B;

Under the current rules, B() is not deleted, but is ill-formed because it calls the deleted ~A::A() if it exits via an exception after the completion of the construction of A. A deleted subobject destructor should be added to the list of reasons for implicit deletion in 12.1 [class.ctor] and 12.8 [class.copy].

and the proposed resolution was to add the bullet you note above and the same wording to the following section 12.1 [class.ctor] paragraph 5:

any direct or virtual base class or non-static data member has a type with a destructor that is deleted or inaccessible from the defaulted default constructor.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740