Deleting object with private destructor

前端 未结 3 1981
后悔当初
后悔当初 2021-01-05 01:24

How is that possible that it is allowed to delete object with private destructor in the following code? I\'ve reduced real program to the following sample, but it still comp

3条回答
  •  情歌与酒
    2021-01-05 01:56

    You are trying to delete object of incomplete class type. C++ Standard says that you'll get undefined behavior in this case (5.3.5/5):

    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.

    To detect such cases you could use boost::checked_delete:

    template 
    inline void checked_delete( T* p )
    {
        typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
        (void) sizeof(type_must_be_complete);
        delete p;
    }
    

提交回复
热议问题