Delete objects of incomplete type

前端 未结 3 2012
孤街浪徒
孤街浪徒 2020-12-06 16:02

This one made me think:

class X;

void foo(X* p)
{
    delete p;
}

How can we possibly delete p if we do not even know whether

3条回答
  •  甜味超标
    2020-12-06 16:51

    From the standard [expr.delete]:

    If the object being deleted has incomplete class type at the point of deletion and the complete class has a non-trivial destructor or a deallocation function, the behavior is undefined.

    So, it's UB if there's nontrivial stuff to do, and it's ok if there isn't. Warnings aren't neccessary for UB.

提交回复
热议问题