Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) boost::checked_delete

 

boost::checked_delete is a compile-time-checked version of delete.

 

To cite from http://www.boost.org/libs/utility/checked_delete.html:

 

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.

 

In other words, if you use a lot of forward declarations you might choose to prefer using boost::checked_delete.

 

Note that std::auto_ptr does not use a checked delete. When you really need a checked delete, use boost::scoped_ptr instead (but note that boost::scoped_ptr has a slightly different interface then std::auto_ptr).

 

 

 

 

 

Befriending a class with boost::checked_delete

 

When you befriending a function template specialization (like boost::checked_delete), always explicitly add at last the <> template syntax [1].

 

struct MyClass
{
  //MyClass's interface
  private:
  ~MyClass() { /* something */ }
  friend void boost::checked_delete  (MyClass* x); //Bad  [1]
  friend void boost::checked_delete<>(MyClass* x); //Good [1]
};

 

 

 

 

 

External links

 

 

 

 

 

 

References

 

  1. Herb Sutter. Exceptional C++ style. 2005. ISBN: 0-201-76042-8. Item 8: 'Befriending templates'.

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict