Is the C++ compiler optimizer allowed to break my destructor ability to be called multiple times?

后端 未结 2 2017
鱼传尺愫
鱼传尺愫 2021-01-06 07:47

We once had an interview with a very experienced C++ developer who couldn\'t answer the following question: is it necessary to call the base class destructor from the de

2条回答
  •  逝去的感伤
    2021-01-06 07:53

    Calling the destructor converts an object into raw memory. You cannot destruct raw memory; this is undefined behaviour. The C++ compiler is entitled to do anything it wants. While it is unlikely that it will turn your computer in cottage cheese, it might deliberately trigger a slap-on-the-wrist SEGFAULT (at least in debug mode).

提交回复
热议问题