Destructor not called after destroying object placement-new'ed

后端 未结 3 830
予麋鹿
予麋鹿 2021-02-19 14:21

I had no clue why this doesn\'t work. The following Function is created by placement new. A function is provided that checks whether it should be destructed, and if

相关标签:
3条回答
  • 2021-02-19 14:53

    This is because ~Function(); in not a destructor call syntactically here. Use this->~Function(); instead.

    ~Function(); is parsed as an operator ~ and creation of the Function object on the stack. Function class has an operator bool that's why this will be compiled.

    0 讨论(0)
  • 2021-02-19 15:00

    Change your explicit destructor call to

    this->~Function();
    

    Currently the ~Function is constructing a "Function" and then calling the ~ bitwise operator, (legal because you have a conversion to bool), and then destructing that, not the called object.

    0 讨论(0)
  • 2021-02-19 15:09

    As I recall the destructor cannot be called explicitely. Try moving the cleanup code from destructor to other function and call it instead.

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