Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of boost::checked_delete

I don't understand the purpose of boost::checked_delete. The documentation says:

The C++ Standard allows, in 5.3.5/5, pointers to incomplete class types to be deleted with a delete-expression. When the class has a non-trivial destructor, or a class-specific operator delete, the behavior is undefined. Some compilers issue a warning when an incomplete type is deleted, but unfortunately, not all do, and programmers sometimes ignore or disable warnings.

The supplied function and class templates can be used to prevent these problems, as they require a complete type, and cause a compilation error otherwise.

So the C++ standard allows you to delete incomplete types, which causes undefined behavior if the type has a non-trivial destructor. What? How can an incomplete type have any destructor at all? Isn't an incomplete type just a prototype?

like image 908
Channel72 Avatar asked May 17 '10 21:05

Channel72


Video Answer


1 Answers

The most common example of an incomplete type is one that has only been declared:

// this file does not include the definition of foo

class foo;

void bad(foo *f)
{
    delete f;  // undefined behavior if there exists foo::~foo
}

In reality, the definition of foo may look like this:

class foo
{
public:
    ~foo() { ... };
};

But if the top code has not 'seen' the class definition and just sees the class declaration, the code will compile.

like image 78
R Samuel Klatchko Avatar answered Sep 23 '22 05:09

R Samuel Klatchko