Why is taking the address of a destructor forbidden?

那年仲夏 提交于 2019-12-21 06:52:18

问题


C++ standard at 12.4.2 states that

[...] The address of a destructor shall not be taken. [...]

However, one can without any complaints by the compiler take the address of a wrapper around a class destructor, like this:

struct Test {
    ~Test(){};

    void destructor(){
        this->~Test();
    }
};

void (Test::*d)() = &Test::destructor;

So what's the rationale behind forbidding to take the address of a destructor directly?


回答1:


Constructors and destructors are somewhat special. The compiler often uses different conventions when calling them (e.g. to pass extra hidden arguments). If you took the address and saved it somewhere, the compiler would lose the information that the function is a constructor or destructor, and would not know to use the special conventions.



来源:https://stackoverflow.com/questions/7905272/why-is-taking-the-address-of-a-destructor-forbidden

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!