Why is taking the address of a destructor forbidde

2019-04-03 05:14发布

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条回答
在下西门庆
2楼-- · 2019-04-03 05:21

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.

查看更多
登录 后发表回答