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

后端 未结 4 1206
时光说笑
时光说笑 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条回答
  •  萌比男神i
    2020-12-01 15:38

    I suppose you might get away with it (on a particular compiler) if

    1. new/delete are implemented in terms of malloc/free and
    2. the placement new actually uses the same mechanism for keeping track of the destructor associated with allocations as the standard new, so that the call to delete could find the right destructor.

    But there is no way it can be portable or safe.

提交回复
热议问题