Legality of using operator delete on a pointer obtained from placement new

后端 未结 4 1213
时光说笑
时光说笑 2020-12-01 15:19

I\'m dang certain that this code ought to be illegal, as it clearly won\'t work, but it seems to be allowed by the C++0x FCD.

class X { /* ... */};
void* raw         


        
4条回答
  •  心在旅途
    2020-12-01 15:47

    The compiler doesn't really care that p comes from a placement new call, so it won't prevent you from issuing delete on the object. In that way, your example can be considered "legal".

    That won't work though, since "placement delete" operators cannot be called explicitly. They're only called implicitly if the constructor throws, so the destructor can run.

提交回复
热议问题