1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
---|
2 | <html> |
---|
3 | <head> |
---|
4 | <title>Boost: checked_delete.hpp documentation</title> |
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
---|
6 | </head> |
---|
7 | <body bgcolor="white" style="MARGIN-LEFT: 5%; MARGIN-RIGHT: 5%"> |
---|
8 | <table border="0" width="100%"> |
---|
9 | <tr> |
---|
10 | <td width="277"> |
---|
11 | <img src="../../boost.png" alt="boost.png (6897 bytes)" width="277" height="86"> |
---|
12 | </td> |
---|
13 | <td align="middle"> |
---|
14 | <h1>checked_delete.hpp</h1> |
---|
15 | </td> |
---|
16 | </tr> |
---|
17 | <tr> |
---|
18 | <td colspan="2" height="64"> </td> |
---|
19 | </tr> |
---|
20 | </table> |
---|
21 | <p> |
---|
22 | The header <STRONG><boost/checked_delete.hpp></STRONG> defines two |
---|
23 | function templates, <STRONG>checked_delete</STRONG> and <STRONG>checked_array_delete</STRONG>, |
---|
24 | and two class templates, <STRONG>checked_deleter</STRONG> and <STRONG>checked_array_deleter</STRONG>. |
---|
25 | </p> |
---|
26 | <P>The C++ Standard allows, in 5.3.5/5, pointers to incomplete class types to be |
---|
27 | deleted with a <EM>delete-expression</EM>. When the class has a non-trivial |
---|
28 | destructor, or a class-specific operator delete, the behavior is undefined. |
---|
29 | Some compilers issue a warning when an incomplete type is deleted, but |
---|
30 | unfortunately, not all do, and programmers sometimes ignore or disable |
---|
31 | warnings.</P> |
---|
32 | <P>A particularly troublesome case is when a smart pointer's destructor, such as <STRONG> |
---|
33 | boost::scoped_ptr<T>::~scoped_ptr</STRONG>, is instantiated with an |
---|
34 | incomplete type. This can often lead to silent, hard to track failures.</P> |
---|
35 | <P>The supplied function and class templates can be used to prevent these problems, |
---|
36 | as they require a complete type, and cause a compilation error otherwise.</P> |
---|
37 | <h3><a name="Synopsis">Synopsis</a></h3> |
---|
38 | <pre> |
---|
39 | namespace boost |
---|
40 | { |
---|
41 | |
---|
42 | template<class T> void checked_delete(T * p); |
---|
43 | template<class T> void checked_array_delete(T * p); |
---|
44 | template<class T> struct checked_deleter; |
---|
45 | template<class T> struct checked_array_deleter; |
---|
46 | |
---|
47 | } |
---|
48 | </pre> |
---|
49 | <h3>checked_delete</h3> |
---|
50 | <h4><a name="checked_delete">template<class T> void checked_delete(T * p);</a></h4> |
---|
51 | <blockquote> |
---|
52 | <p> |
---|
53 | <b>Requires:</b> <b>T</b> must be a complete type. The expression <tt>delete p</tt> |
---|
54 | must be well-formed. |
---|
55 | </p> |
---|
56 | <p> |
---|
57 | <b>Effects:</b> <tt>delete p;</tt> |
---|
58 | </p> |
---|
59 | </blockquote> |
---|
60 | <h3>checked_array_delete</h3> |
---|
61 | <h4><a name="checked_array_delete">template<class T> void checked_array_delete(T |
---|
62 | * p);</a></h4> |
---|
63 | <blockquote> |
---|
64 | <p> |
---|
65 | <b>Requires:</b> <b>T</b> must be a complete type. The expression <tt>delete [] p</tt> |
---|
66 | must be well-formed. |
---|
67 | </p> |
---|
68 | <p> |
---|
69 | <b>Effects:</b> <tt>delete [] p;</tt> |
---|
70 | </p> |
---|
71 | </blockquote> |
---|
72 | <h3>checked_deleter</h3> |
---|
73 | <pre> |
---|
74 | template<class T> struct checked_deleter |
---|
75 | { |
---|
76 | typedef void result_type; |
---|
77 | typedef T * argument_type; |
---|
78 | void operator()(T * p) const; |
---|
79 | }; |
---|
80 | </pre> |
---|
81 | <h4>void checked_deleter<T>::operator()(T * p) const;</h4> |
---|
82 | <blockquote> |
---|
83 | <p> |
---|
84 | <b>Requires:</b> <b>T</b> must be a complete type. The expression <tt>delete p</tt> |
---|
85 | must be well-formed. |
---|
86 | </p> |
---|
87 | <p> |
---|
88 | <b>Effects:</b> <tt>delete p;</tt> |
---|
89 | </p> |
---|
90 | </blockquote> |
---|
91 | <h3>checked_array_deleter</h3> |
---|
92 | <pre> |
---|
93 | template<class T> struct checked_array_deleter |
---|
94 | { |
---|
95 | typedef void result_type; |
---|
96 | typedef T * argument_type; |
---|
97 | void operator()(T * p) const; |
---|
98 | }; |
---|
99 | </pre> |
---|
100 | <h4>void checked_array_deleter<T>::operator()(T * p) const;</h4> |
---|
101 | <blockquote> |
---|
102 | <p> |
---|
103 | <b>Requires:</b> <b>T</b> must be a complete type. The expression <tt>delete [] p</tt> |
---|
104 | must be well-formed. |
---|
105 | </p> |
---|
106 | <p> |
---|
107 | <b>Effects:</b> <tt>delete [] p;</tt> |
---|
108 | </p> |
---|
109 | </blockquote> |
---|
110 | <h3><a name="Acknowledgements">Acknowledgements</a></h3> |
---|
111 | <p> |
---|
112 | The function templates <STRONG>checked_delete</STRONG> and <STRONG>checked_array_delete</STRONG> |
---|
113 | were originally part of <STRONG><boost/utility.hpp></STRONG>, and the |
---|
114 | documentation acknowledged Beman Dawes, Dave Abrahams, Vladimir Prus, Rainer |
---|
115 | Deyke, John Maddock, and others as contributors. |
---|
116 | </p> |
---|
117 | <p> |
---|
118 | <br> |
---|
119 | <small>Copyright © 2002 by Peter Dimov. Permission to copy, use, modify, sell and |
---|
120 | distribute this document is granted provided this copyright notice appears in |
---|
121 | all copies. This document is provided "as is" without express or implied |
---|
122 | warranty, and with no claim as to its suitability for any purpose.</small></p> |
---|
123 | </body> |
---|
124 | </html> |
---|