2

What does the implicitly defined destructor do? Is it just an empty function that is defined by the compiler?

struct Foo
{
  int i;
};

struct Bar
{
  int i;
  ~Bar()
  { 
    // empty...
  }
};

Is the destruction of Foo identical to Bar? Or does the implicit destructor do something inside of the compiler generated body?

curiousguy
  • 8,038
  • 2
  • 40
  • 58
Krystian S
  • 1,586
  • 7
  • 23

1 Answers1

3

What does the implicitly defined destructor do?

It will be the same as an explicitly defined destructor with an empty body. In effect, it destroys all sub-objects and does nothing more.

Is it just an empty function that is defined by the compiler?

In practice, there may not even need to be an empty function. But it may be useful to to think that there is when thinking in terms of the abstract machine.

Or does the implicit destructor do something inside of the compiler generated body?

Depends on the class. In the case of Foo, nothing needs to be done by the destructor.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • You might want to go into triviality, and why it is important. – Deduplicator Dec 11 '18 at 03:02
  • @Deduplicator OK, `Bar`'s destructor is not trivial because it is user-defined, even though it is empty. Technically one must destroy a `Bar` with `delete`, while a `Foo`'s memory can simply be de-allocated, even though `Bar`'s destructor doesn't do anything either. Anything else? – Peter - Reinstate Monica Dec 11 '18 at 03:09
  • @PeterA.Schneider Technically, one must *call the dtor* (which is *part* of what a delete-expression does). And user-code can tell whether the dtor it's trivial, and automatically optimize in that case. – Deduplicator Dec 11 '18 at 20:56