Why am I permitted to declare an object with a deleted destructor?

前端 未结 4 1383
甜味超标
甜味超标 2021-01-01 13:59

Consider the following text:

[C++11: 12.4/11]: Destructors are invoked implicitly

  • for constructed objects with static
相关标签:
4条回答
  • 2021-01-01 14:37

    The first part is not ill-formed because the standard text doesn't apply - no object of type A is declared there.

    For the second part, let's review how object construction works. The standard says (15.2/2) that if any part of construction throws, all fully constructed subobjects up to that point are destroyed in reverse order of construction.

    This means that the code underlying a constructor, if all written out by hand, would look something like this:

    // Given:
    struct C : A, B {
       D d;
       C() : A(), B(), d() { /* more code */ }
    };
    
    // This is the expanded constructor:
    C() {
      A();
      try {
        B();
        try {
          d.D();
          try {
            /* more code */
          } catch(...) { d.~D(); throw; }
        } catch(...) { ~B(); throw; }
      } catch(...) { ~A(); throw; }
    }
    

    For your simpler class, the expanded code for the default constructor (whose definition is required by the new expression) would look like this:

    B::B() {
      A();
      try {
        // nothing to do here
      } catch(...) {
        ~A(); // error: ~A() is deleted.
        throw;
      }
    }
    

    Making this work for cases where no exception can possibly be thrown after initialization for some subobject has been completed is just too complex to specify. Therefore, this doesn't actually happen, because the default constructor for B is implicitly defined as deleted in the first place, due to the last bullet point in N3797 12.1/4:

    A defaulted default constructor for class X is defined as deleted if:

    • [...]
    • 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.

    The equivalent language exists for copy/move constructors as the fourth bullet in 12.8/11.

    There is also an important paragraph in 12.6.2/10:

    In a non-delegating constructor, the destructor for each direct or virtual base class and for each non-static data member of class type is potentially invoked.

    0 讨论(0)
  • 2021-01-01 14:38

    Accessibility is orthogonal to deletedness:

    [C++11: 11.2/1]: If a class is declared to be a base class (Clause 10) for another class using the public access specifier, the public members of the base class are accessible as public members of the derived class and protected members of the base class are accessible as protected members of the derived class. If a class is declared to be a base class for another class using the protected access specifier, the public and protected members of the base class are accessible as protected members of the derived class. If a class is declared to be a base class for another class using the private access specifier, the public and protected members of the base class are accessible as private members of the derived class.

    There is this:

    [C++11: 8.4.3/2]: A program that refers to a deleted function implicitly or explicitly, other than to declare it, is ill-formed. [ Note: This includes calling the function implicitly or explicitly and forming a pointer or pointer-to-member to the function. It applies even for references in expressions that are not potentially-evaluated. If a function is overloaded, it is referenced only if the function is selected by overload resolution. —end note ]

    But you never "refer to" the deleted destructor.

    (I still can't explain why the inheritance example doesn't compile.)

    0 讨论(0)
  • 2021-01-01 14:43

    My guess is that this is what happens.

    The implicitly generated B() constructor will first of all construct its base class subobject of type A. The language then states that if an exception is thrown during execution of the body of the B() constructor, the A subobject must be destroyed. Hence the need to access the deleted ~A() - it is formally needed for when the constructor throws. Of course, since the generated body of B() is empty this can never ever happen, but the requirement that ~A() should be accessible is still there.

    Of course, this is 1) just a guess from my side of why there is an error in the first place and 2) not in any way a quote of the standardese to say whether this would actually be formally ill-formed or just an implementation detail in gcc. Could perhaps give you a clue of where in the standard to look though...

    0 讨论(0)
  • 2021-01-01 14:48

    It's that B's destructor is generated by the compiler at the line of your error and it has a call to A's destructor which is deleted, hence the error. In the first example nothing is trying to call A's destructor hence no error.

    0 讨论(0)
提交回复
热议问题