Why, really, deleting an incomplete type is undefined behaviour?

后端 未结 6 1771
北荒
北荒 2020-11-30 06:07

Consider this classic example used to explain what not to do with forward declarations:

//in Handle.h file
class Body;

class Handle
{
   public:
           


        
6条回答
  •  无人及你
    2020-11-30 06:13

    I'm just guessing, but perhaps it has to do with the ability of per-class allocation operators.

    That is:

    struct foo
    {
        void* operator new(size_t);
        void operator delete(void*);
    };
    
    // in another header, like your example
    
    struct foo;
    
    struct bar
    {
        bar();
        ~bar() { delete myFoo; }
    
        foo* myFoo;
    };
    
    // in translation unit
    
    #include "bar.h"
    #include "foo.h"
    
    bar::bar() :
    myFoo(new foo) // uses foo::operator new
    {}
    
    // but destructor uses global...!!
    

    And now we've mismatched the allocation operators, and entered undefined behavior. The only way to guarantee that can't happen is to say "make the type complete". Otherwise, it's impossible to ensure.

提交回复
热议问题