c++visual-c++compiler-warningsdelete-operator

Are there real life cases when deleting an incomplete type is not an error in C++?


Visual C++ has C4150 warning for cases when delete is applied to a pointer to incomplete type.

Such cases yield undefined behavior according to the Standard. AFAIK in Visual C++ they result in default operator delete() function and no destructor being called which allows for numerous bugs.

Now I could have used #prarma warning( error : 4150 ) in Visual C++ to treat that warning as error. I guess there're reasons why it is a warning and not an error by default in Visual C++.

In what real life code would I want to allow such cases? Why would I not switch that warning into a compiler error?


Solution

  • It's not always an UB.

    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.