5 <!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
6 <meta name="viewport" content="width=device-width, initial-scale=1.0">
7 <meta name="generator" content="Asciidoctor 1.5.8">
8 <meta name="author" content="Greg Colvin, Beman Dawes, Peter Dimov, Glen Fernandes">
9 <title>Boost.SmartPtr: The Smart Pointer Library</title>
10 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
12 /* Asciidoctor default stylesheet | MIT License | http://asciidoctor.org */
13 /* Uncomment @import statement below to use as custom stylesheet */
14 /*@import "https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700";*/
15 article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section,summary{display:block}
16 audio,canvas,video{display:inline-block}
17 audio:not([controls]){display:none;height:0}
18 script{display:none!important}
19 html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}
20 a{background:transparent}
21 a:focus{outline:thin dotted}
22 a:active,a:hover{outline:0}
23 h1{font-size:2em;margin:.67em 0}
24 abbr[title]{border-bottom:1px dotted}
25 b,strong{font-weight:bold}
26 dfn{font-style:italic}
27 hr{-moz-box-sizing:content-box;box-sizing:content-box;height:0}
28 mark{background:#ff0;color:#000}
29 code,kbd,pre,samp{font-family:monospace;font-size:1em}
30 pre{white-space:pre-wrap}
31 q{quotes:"\201C" "\201D" "\2018" "\2019"}
33 sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}
37 svg:not(:root){overflow:hidden}
39 fieldset{border:1px solid silver;margin:0 2px;padding:.35em .625em .75em}
40 legend{border:0;padding:0}
41 button,input,select,textarea{font-family:inherit;font-size:100%;margin:0}
42 button,input{line-height:normal}
43 button,select{text-transform:none}
44 button,html input[type="button"],input[type="reset"],input[type="submit"]{-webkit-appearance:button;cursor:pointer}
45 button[disabled],html input[disabled]{cursor:default}
46 input[type="checkbox"],input[type="radio"]{box-sizing:border-box;padding:0}
47 button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0}
48 textarea{overflow:auto;vertical-align:top}
49 table{border-collapse:collapse;border-spacing:0}
50 *,*::before,*::after{-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box}
51 html,body{font-size:100%}
52 body{background:#fff;color:rgba(0,0,0,.8);padding:0;margin:0;font-family:"Noto Serif","DejaVu Serif",serif;font-weight:400;font-style:normal;line-height:1;position:relative;cursor:auto;tab-size:4;-moz-osx-font-smoothing:grayscale;-webkit-font-smoothing:antialiased}
53 a:hover{cursor:pointer}
54 img,object,embed{max-width:100%;height:auto}
55 object,embed{height:100%}
56 img{-ms-interpolation-mode:bicubic}
57 .left{float:left!important}
58 .right{float:right!important}
59 .text-left{text-align:left!important}
60 .text-right{text-align:right!important}
61 .text-center{text-align:center!important}
62 .text-justify{text-align:justify!important}
64 img,object,svg{display:inline-block;vertical-align:middle}
65 textarea{height:auto;min-height:50px}
67 .center{margin-left:auto;margin-right:auto}
69 .subheader,.admonitionblock td.content>.title,.audioblock>.title,.exampleblock>.title,.imageblock>.title,.listingblock>.title,.literalblock>.title,.stemblock>.title,.openblock>.title,.paragraph>.title,.quoteblock>.title,table.tableblock>.title,.verseblock>.title,.videoblock>.title,.dlist>.title,.olist>.title,.ulist>.title,.qlist>.title,.hdlist>.title{line-height:1.45;color:#7a2518;font-weight:400;margin-top:0;margin-bottom:.25em}
70 div,dl,dt,dd,ul,ol,li,h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6,pre,form,p,blockquote,th,td{margin:0;padding:0;direction:ltr}
71 a{color:#2156a5;text-decoration:underline;line-height:inherit}
72 a:hover,a:focus{color:#1d4b8f}
74 p{font-family:inherit;font-weight:400;font-size:1em;line-height:1.6;margin-bottom:1.25em;text-rendering:optimizeLegibility}
75 p aside{font-size:.875em;line-height:1.35;font-style:italic}
76 h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6{font-family:"Open Sans","DejaVu Sans",sans-serif;font-weight:300;font-style:normal;color:#ba3925;text-rendering:optimizeLegibility;margin-top:1em;margin-bottom:.5em;line-height:1.0125em}
77 h1 small,h2 small,h3 small,#toctitle small,.sidebarblock>.content>.title small,h4 small,h5 small,h6 small{font-size:60%;color:#e99b8f;line-height:0}
79 h2{font-size:1.6875em}
80 h3,#toctitle,.sidebarblock>.content>.title{font-size:1.375em}
81 h4,h5{font-size:1.125em}
83 hr{border:solid #dddddf;border-width:1px 0 0;clear:both;margin:1.25em 0 1.1875em;height:0}
84 em,i{font-style:italic;line-height:inherit}
85 strong,b{font-weight:bold;line-height:inherit}
86 small{font-size:60%;line-height:inherit}
87 code{font-family:"Droid Sans Mono","DejaVu Sans Mono",monospace;font-weight:400;color:rgba(0,0,0,.9)}
88 ul,ol,dl{font-size:1em;line-height:1.6;margin-bottom:1.25em;list-style-position:outside;font-family:inherit}
89 ul,ol{margin-left:1.5em}
90 ul li ul,ul li ol{margin-left:1.25em;margin-bottom:0;font-size:1em}
91 ul.square li ul,ul.circle li ul,ul.disc li ul{list-style:inherit}
92 ul.square{list-style-type:square}
93 ul.circle{list-style-type:circle}
94 ul.disc{list-style-type:disc}
95 ol li ul,ol li ol{margin-left:1.25em;margin-bottom:0}
96 dl dt{margin-bottom:.3125em;font-weight:bold}
97 dl dd{margin-bottom:1.25em}
98 abbr,acronym{text-transform:uppercase;font-size:90%;color:rgba(0,0,0,.8);border-bottom:1px dotted #ddd;cursor:help}
99 abbr{text-transform:none}
100 blockquote{margin:0 0 1.25em;padding:.5625em 1.25em 0 1.1875em;border-left:1px solid #ddd}
101 blockquote cite{display:block;font-size:.9375em;color:rgba(0,0,0,.6)}
102 blockquote cite::before{content:"\2014 \0020"}
103 blockquote cite a,blockquote cite a:visited{color:rgba(0,0,0,.6)}
104 blockquote,blockquote p{line-height:1.6;color:rgba(0,0,0,.85)}
105 @media screen and (min-width:768px){h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6{line-height:1.2}
107 h2{font-size:2.3125em}
108 h3,#toctitle,.sidebarblock>.content>.title{font-size:1.6875em}
109 h4{font-size:1.4375em}}
110 table{background:#fff;margin-bottom:1.25em;border:solid 1px #dedede}
111 table thead,table tfoot{background:#f7f8f7}
112 table thead tr th,table thead tr td,table tfoot tr th,table tfoot tr td{padding:.5em .625em .625em;font-size:inherit;color:rgba(0,0,0,.8);text-align:left}
113 table tr th,table tr td{padding:.5625em .625em;font-size:inherit;color:rgba(0,0,0,.8)}
114 table tr.even,table tr.alt,table tr:nth-of-type(even){background:#f8f8f7}
115 table thead tr th,table tfoot tr th,table tbody tr td,table tr td,table tfoot tr td{display:table-cell;line-height:1.6}
116 h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6{line-height:1.2;word-spacing:-.05em}
117 h1 strong,h2 strong,h3 strong,#toctitle strong,.sidebarblock>.content>.title strong,h4 strong,h5 strong,h6 strong{font-weight:400}
118 .clearfix::before,.clearfix::after,.float-group::before,.float-group::after{content:" ";display:table}
119 .clearfix::after,.float-group::after{clear:both}
120 *:not(pre)>code{font-size:.9375em;font-style:normal!important;letter-spacing:0;padding:.1em .5ex;word-spacing:-.15em;background-color:#f7f7f8;-webkit-border-radius:4px;border-radius:4px;line-height:1.45;text-rendering:optimizeSpeed;word-wrap:break-word}
121 *:not(pre)>code.nobreak{word-wrap:normal}
122 *:not(pre)>code.nowrap{white-space:nowrap}
123 pre,pre>code{line-height:1.45;color:rgba(0,0,0,.9);font-family:"Droid Sans Mono","DejaVu Sans Mono",monospace;font-weight:400;text-rendering:optimizeSpeed}
124 em em{font-style:normal}
125 strong strong{font-weight:400}
126 .keyseq{color:rgba(51,51,51,.8)}
127 kbd{font-family:"Droid Sans Mono","DejaVu Sans Mono",monospace;display:inline-block;color:rgba(0,0,0,.8);font-size:.65em;line-height:1.45;background-color:#f7f7f7;border:1px solid #ccc;-webkit-border-radius:3px;border-radius:3px;-webkit-box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em white inset;box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em #fff inset;margin:0 .15em;padding:.2em .5em;vertical-align:middle;position:relative;top:-.1em;white-space:nowrap}
128 .keyseq kbd:first-child{margin-left:0}
129 .keyseq kbd:last-child{margin-right:0}
130 .menuseq,.menuref{color:#000}
131 .menuseq b:not(.caret),.menuref{font-weight:inherit}
132 .menuseq{word-spacing:-.02em}
133 .menuseq b.caret{font-size:1.25em;line-height:.8}
134 .menuseq i.caret{font-weight:bold;text-align:center;width:.45em}
135 b.button::before,b.button::after{position:relative;top:-1px;font-weight:400}
136 b.button::before{content:"[";padding:0 3px 0 2px}
137 b.button::after{content:"]";padding:0 2px 0 3px}
138 p a>code:hover{color:rgba(0,0,0,.9)}
139 #header,#content,#footnotes,#footer{width:100%;margin-left:auto;margin-right:auto;margin-top:0;margin-bottom:0;max-width:62.5em;*zoom:1;position:relative;padding-left:.9375em;padding-right:.9375em}
140 #header::before,#header::after,#content::before,#content::after,#footnotes::before,#footnotes::after,#footer::before,#footer::after{content:" ";display:table}
141 #header::after,#content::after,#footnotes::after,#footer::after{clear:both}
142 #content{margin-top:1.25em}
143 #content::before{content:none}
144 #header>h1:first-child{color:rgba(0,0,0,.85);margin-top:2.25rem;margin-bottom:0}
145 #header>h1:first-child+#toc{margin-top:8px;border-top:1px solid #dddddf}
146 #header>h1:only-child,body.toc2 #header>h1:nth-last-child(2){border-bottom:1px solid #dddddf;padding-bottom:8px}
147 #header .details{border-bottom:1px solid #dddddf;line-height:1.45;padding-top:.25em;padding-bottom:.25em;padding-left:.25em;color:rgba(0,0,0,.6);display:-ms-flexbox;display:-webkit-flex;display:flex;-ms-flex-flow:row wrap;-webkit-flex-flow:row wrap;flex-flow:row wrap}
148 #header .details span:first-child{margin-left:-.125em}
149 #header .details span.email a{color:rgba(0,0,0,.85)}
150 #header .details br{display:none}
151 #header .details br+span::before{content:"\00a0\2013\00a0"}
152 #header .details br+span.author::before{content:"\00a0\22c5\00a0";color:rgba(0,0,0,.85)}
153 #header .details br+span#revremark::before{content:"\00a0|\00a0"}
154 #header #revnumber{text-transform:capitalize}
155 #header #revnumber::after{content:"\00a0"}
156 #content>h1:first-child:not([class]){color:rgba(0,0,0,.85);border-bottom:1px solid #dddddf;padding-bottom:8px;margin-top:0;padding-top:1rem;margin-bottom:1.25rem}
157 #toc{border-bottom:1px solid #e7e7e9;padding-bottom:.5em}
158 #toc>ul{margin-left:.125em}
159 #toc ul.sectlevel0>li>a{font-style:italic}
160 #toc ul.sectlevel0 ul.sectlevel1{margin:.5em 0}
161 #toc ul{font-family:"Open Sans","DejaVu Sans",sans-serif;list-style-type:none}
162 #toc li{line-height:1.3334;margin-top:.3334em}
163 #toc a{text-decoration:none}
164 #toc a:active{text-decoration:underline}
165 #toctitle{color:#7a2518;font-size:1.2em}
166 @media screen and (min-width:768px){#toctitle{font-size:1.375em}
167 body.toc2{padding-left:15em;padding-right:0}
168 #toc.toc2{margin-top:0!important;background-color:#f8f8f7;position:fixed;width:15em;left:0;top:0;border-right:1px solid #e7e7e9;border-top-width:0!important;border-bottom-width:0!important;z-index:1000;padding:1.25em 1em;height:100%;overflow:auto}
169 #toc.toc2 #toctitle{margin-top:0;margin-bottom:.8rem;font-size:1.2em}
170 #toc.toc2>ul{font-size:.9em;margin-bottom:0}
171 #toc.toc2 ul ul{margin-left:0;padding-left:1em}
172 #toc.toc2 ul.sectlevel0 ul.sectlevel1{padding-left:0;margin-top:.5em;margin-bottom:.5em}
173 body.toc2.toc-right{padding-left:0;padding-right:15em}
174 body.toc2.toc-right #toc.toc2{border-right-width:0;border-left:1px solid #e7e7e9;left:auto;right:0}}
175 @media screen and (min-width:1280px){body.toc2{padding-left:20em;padding-right:0}
176 #toc.toc2{width:20em}
177 #toc.toc2 #toctitle{font-size:1.375em}
178 #toc.toc2>ul{font-size:.95em}
179 #toc.toc2 ul ul{padding-left:1.25em}
180 body.toc2.toc-right{padding-left:0;padding-right:20em}}
181 #content #toc{border-style:solid;border-width:1px;border-color:#e0e0dc;margin-bottom:1.25em;padding:1.25em;background:#f8f8f7;-webkit-border-radius:4px;border-radius:4px}
182 #content #toc>:first-child{margin-top:0}
183 #content #toc>:last-child{margin-bottom:0}
184 #footer{max-width:100%;background-color:rgba(0,0,0,.8);padding:1.25em}
185 #footer-text{color:rgba(255,255,255,.8);line-height:1.44}
186 #content{margin-bottom:.625em}
187 .sect1{padding-bottom:.625em}
188 @media screen and (min-width:768px){#content{margin-bottom:1.25em}
189 .sect1{padding-bottom:1.25em}}
190 .sect1:last-child{padding-bottom:0}
191 .sect1+.sect1{border-top:1px solid #e7e7e9}
192 #content h1>a.anchor,h2>a.anchor,h3>a.anchor,#toctitle>a.anchor,.sidebarblock>.content>.title>a.anchor,h4>a.anchor,h5>a.anchor,h6>a.anchor{position:absolute;z-index:1001;width:1.5ex;margin-left:-1.5ex;display:block;text-decoration:none!important;visibility:hidden;text-align:center;font-weight:400}
193 #content h1>a.anchor::before,h2>a.anchor::before,h3>a.anchor::before,#toctitle>a.anchor::before,.sidebarblock>.content>.title>a.anchor::before,h4>a.anchor::before,h5>a.anchor::before,h6>a.anchor::before{content:"\00A7";font-size:.85em;display:block;padding-top:.1em}
194 #content h1:hover>a.anchor,#content h1>a.anchor:hover,h2:hover>a.anchor,h2>a.anchor:hover,h3:hover>a.anchor,#toctitle:hover>a.anchor,.sidebarblock>.content>.title:hover>a.anchor,h3>a.anchor:hover,#toctitle>a.anchor:hover,.sidebarblock>.content>.title>a.anchor:hover,h4:hover>a.anchor,h4>a.anchor:hover,h5:hover>a.anchor,h5>a.anchor:hover,h6:hover>a.anchor,h6>a.anchor:hover{visibility:visible}
195 #content h1>a.link,h2>a.link,h3>a.link,#toctitle>a.link,.sidebarblock>.content>.title>a.link,h4>a.link,h5>a.link,h6>a.link{color:#ba3925;text-decoration:none}
196 #content h1>a.link:hover,h2>a.link:hover,h3>a.link:hover,#toctitle>a.link:hover,.sidebarblock>.content>.title>a.link:hover,h4>a.link:hover,h5>a.link:hover,h6>a.link:hover{color:#a53221}
197 .audioblock,.imageblock,.literalblock,.listingblock,.stemblock,.videoblock{margin-bottom:1.25em}
198 .admonitionblock td.content>.title,.audioblock>.title,.exampleblock>.title,.imageblock>.title,.listingblock>.title,.literalblock>.title,.stemblock>.title,.openblock>.title,.paragraph>.title,.quoteblock>.title,table.tableblock>.title,.verseblock>.title,.videoblock>.title,.dlist>.title,.olist>.title,.ulist>.title,.qlist>.title,.hdlist>.title{text-rendering:optimizeLegibility;text-align:left;font-family:"Noto Serif","DejaVu Serif",serif;font-size:1rem;font-style:italic}
199 table.tableblock.fit-content>caption.title{white-space:nowrap;width:0}
200 .paragraph.lead>p,#preamble>.sectionbody>[class="paragraph"]:first-of-type p{font-size:1.21875em;line-height:1.6;color:rgba(0,0,0,.85)}
201 table.tableblock #preamble>.sectionbody>[class="paragraph"]:first-of-type p{font-size:inherit}
202 .admonitionblock>table{border-collapse:separate;border:0;background:none;width:100%}
203 .admonitionblock>table td.icon{text-align:center;width:80px}
204 .admonitionblock>table td.icon img{max-width:none}
205 .admonitionblock>table td.icon .title{font-weight:bold;font-family:"Open Sans","DejaVu Sans",sans-serif;text-transform:uppercase}
206 .admonitionblock>table td.content{padding-left:1.125em;padding-right:1.25em;border-left:1px solid #dddddf;color:rgba(0,0,0,.6)}
207 .admonitionblock>table td.content>:last-child>:last-child{margin-bottom:0}
208 .exampleblock>.content{border-style:solid;border-width:1px;border-color:#e6e6e6;margin-bottom:1.25em;padding:1.25em;background:#fff;-webkit-border-radius:4px;border-radius:4px}
209 .exampleblock>.content>:first-child{margin-top:0}
210 .exampleblock>.content>:last-child{margin-bottom:0}
211 .sidebarblock{border-style:solid;border-width:1px;border-color:#e0e0dc;margin-bottom:1.25em;padding:1.25em;background:#f8f8f7;-webkit-border-radius:4px;border-radius:4px}
212 .sidebarblock>:first-child{margin-top:0}
213 .sidebarblock>:last-child{margin-bottom:0}
214 .sidebarblock>.content>.title{color:#7a2518;margin-top:0;text-align:center}
215 .exampleblock>.content>:last-child>:last-child,.exampleblock>.content .olist>ol>li:last-child>:last-child,.exampleblock>.content .ulist>ul>li:last-child>:last-child,.exampleblock>.content .qlist>ol>li:last-child>:last-child,.sidebarblock>.content>:last-child>:last-child,.sidebarblock>.content .olist>ol>li:last-child>:last-child,.sidebarblock>.content .ulist>ul>li:last-child>:last-child,.sidebarblock>.content .qlist>ol>li:last-child>:last-child{margin-bottom:0}
216 .literalblock pre,.listingblock pre:not(.highlight),.listingblock pre[class="highlight"],.listingblock pre[class^="highlight "],.listingblock pre.CodeRay,.listingblock pre.prettyprint{background:#f7f7f8}
217 .sidebarblock .literalblock pre,.sidebarblock .listingblock pre:not(.highlight),.sidebarblock .listingblock pre[class="highlight"],.sidebarblock .listingblock pre[class^="highlight "],.sidebarblock .listingblock pre.CodeRay,.sidebarblock .listingblock pre.prettyprint{background:#f2f1f1}
218 .literalblock pre,.literalblock pre[class],.listingblock pre,.listingblock pre[class]{-webkit-border-radius:4px;border-radius:4px;word-wrap:break-word;overflow-x:auto;padding:1em;font-size:.8125em}
219 @media screen and (min-width:768px){.literalblock pre,.literalblock pre[class],.listingblock pre,.listingblock pre[class]{font-size:.90625em}}
220 @media screen and (min-width:1280px){.literalblock pre,.literalblock pre[class],.listingblock pre,.listingblock pre[class]{font-size:1em}}
221 .literalblock pre.nowrap,.literalblock pre.nowrap pre,.listingblock pre.nowrap,.listingblock pre.nowrap pre{white-space:pre;word-wrap:normal}
222 .literalblock.output pre{color:#f7f7f8;background-color:rgba(0,0,0,.9)}
223 .listingblock pre.highlightjs{padding:0}
224 .listingblock pre.highlightjs>code{padding:1em;-webkit-border-radius:4px;border-radius:4px}
225 .listingblock pre.prettyprint{border-width:0}
226 .listingblock>.content{position:relative}
227 .listingblock code[data-lang]::before{display:none;content:attr(data-lang);position:absolute;font-size:.75em;top:.425rem;right:.5rem;line-height:1;text-transform:uppercase;color:#999}
228 .listingblock:hover code[data-lang]::before{display:block}
229 .listingblock.terminal pre .command::before{content:attr(data-prompt);padding-right:.5em;color:#999}
230 .listingblock.terminal pre .command:not([data-prompt])::before{content:"$"}
231 table.pyhltable{border-collapse:separate;border:0;margin-bottom:0;background:none}
232 table.pyhltable td{vertical-align:top;padding-top:0;padding-bottom:0;line-height:1.45}
233 table.pyhltable td.code{padding-left:.75em;padding-right:0}
234 pre.pygments .lineno,table.pyhltable td:not(.code){color:#999;padding-left:0;padding-right:.5em;border-right:1px solid #dddddf}
235 pre.pygments .lineno{display:inline-block;margin-right:.25em}
236 table.pyhltable .linenodiv{background:none!important;padding-right:0!important}
237 .quoteblock{margin:0 1em 1.25em 1.5em;display:table}
238 .quoteblock>.title{margin-left:-1.5em;margin-bottom:.75em}
239 .quoteblock blockquote,.quoteblock p{color:rgba(0,0,0,.85);font-size:1.15rem;line-height:1.75;word-spacing:.1em;letter-spacing:0;font-style:italic;text-align:justify}
240 .quoteblock blockquote{margin:0;padding:0;border:0}
241 .quoteblock blockquote::before{content:"\201c";float:left;font-size:2.75em;font-weight:bold;line-height:.6em;margin-left:-.6em;color:#7a2518;text-shadow:0 1px 2px rgba(0,0,0,.1)}
242 .quoteblock blockquote>.paragraph:last-child p{margin-bottom:0}
243 .quoteblock .attribution{margin-top:.75em;margin-right:.5ex;text-align:right}
244 .verseblock{margin:0 1em 1.25em}
245 .verseblock pre{font-family:"Open Sans","DejaVu Sans",sans;font-size:1.15rem;color:rgba(0,0,0,.85);font-weight:300;text-rendering:optimizeLegibility}
246 .verseblock pre strong{font-weight:400}
247 .verseblock .attribution{margin-top:1.25rem;margin-left:.5ex}
248 .quoteblock .attribution,.verseblock .attribution{font-size:.9375em;line-height:1.45;font-style:italic}
249 .quoteblock .attribution br,.verseblock .attribution br{display:none}
250 .quoteblock .attribution cite,.verseblock .attribution cite{display:block;letter-spacing:-.025em;color:rgba(0,0,0,.6)}
251 .quoteblock.abstract blockquote::before,.quoteblock.excerpt blockquote::before,.quoteblock .quoteblock blockquote::before{display:none}
252 .quoteblock.abstract blockquote,.quoteblock.abstract p,.quoteblock.excerpt blockquote,.quoteblock.excerpt p,.quoteblock .quoteblock blockquote,.quoteblock .quoteblock p{line-height:1.6;word-spacing:0}
253 .quoteblock.abstract{margin:0 1em 1.25em;display:block}
254 .quoteblock.abstract>.title{margin:0 0 .375em;font-size:1.15em;text-align:center}
255 .quoteblock.excerpt,.quoteblock .quoteblock{margin:0 0 1.25em;padding:0 0 .25em 1em;border-left:.25em solid #dddddf}
256 .quoteblock.excerpt blockquote,.quoteblock.excerpt p,.quoteblock .quoteblock blockquote,.quoteblock .quoteblock p{color:inherit;font-size:1.0625rem}
257 .quoteblock.excerpt .attribution,.quoteblock .quoteblock .attribution{color:inherit;text-align:left;margin-right:0}
258 table.tableblock{max-width:100%;border-collapse:separate}
259 p.tableblock:last-child{margin-bottom:0}
260 td.tableblock>.content{margin-bottom:-1.25em}
261 table.tableblock,th.tableblock,td.tableblock{border:0 solid #dedede}
262 table.grid-all>thead>tr>.tableblock,table.grid-all>tbody>tr>.tableblock{border-width:0 1px 1px 0}
263 table.grid-all>tfoot>tr>.tableblock{border-width:1px 1px 0 0}
264 table.grid-cols>*>tr>.tableblock{border-width:0 1px 0 0}
265 table.grid-rows>thead>tr>.tableblock,table.grid-rows>tbody>tr>.tableblock{border-width:0 0 1px}
266 table.grid-rows>tfoot>tr>.tableblock{border-width:1px 0 0}
267 table.grid-all>*>tr>.tableblock:last-child,table.grid-cols>*>tr>.tableblock:last-child{border-right-width:0}
268 table.grid-all>tbody>tr:last-child>.tableblock,table.grid-all>thead:last-child>tr>.tableblock,table.grid-rows>tbody>tr:last-child>.tableblock,table.grid-rows>thead:last-child>tr>.tableblock{border-bottom-width:0}
269 table.frame-all{border-width:1px}
270 table.frame-sides{border-width:0 1px}
271 table.frame-topbot,table.frame-ends{border-width:1px 0}
272 table.stripes-all tr,table.stripes-odd tr:nth-of-type(odd){background:#f8f8f7}
273 table.stripes-none tr,table.stripes-odd tr:nth-of-type(even){background:none}
274 th.halign-left,td.halign-left{text-align:left}
275 th.halign-right,td.halign-right{text-align:right}
276 th.halign-center,td.halign-center{text-align:center}
277 th.valign-top,td.valign-top{vertical-align:top}
278 th.valign-bottom,td.valign-bottom{vertical-align:bottom}
279 th.valign-middle,td.valign-middle{vertical-align:middle}
280 table thead th,table tfoot th{font-weight:bold}
281 tbody tr th{display:table-cell;line-height:1.6;background:#f7f8f7}
282 tbody tr th,tbody tr th p,tfoot tr th,tfoot tr th p{color:rgba(0,0,0,.8);font-weight:bold}
283 p.tableblock>code:only-child{background:none;padding:0}
284 p.tableblock{font-size:1em}
285 td>div.verse{white-space:pre}
286 ol{margin-left:1.75em}
287 ul li ol{margin-left:1.5em}
288 dl dd{margin-left:1.125em}
289 dl dd:last-child,dl dd:last-child>:last-child{margin-bottom:0}
290 ol>li p,ul>li p,ul dd,ol dd,.olist .olist,.ulist .ulist,.ulist .olist,.olist .ulist{margin-bottom:.625em}
291 ul.checklist,ul.none,ol.none,ul.no-bullet,ol.no-bullet,ol.unnumbered,ul.unstyled,ol.unstyled{list-style-type:none}
292 ul.no-bullet,ol.no-bullet,ol.unnumbered{margin-left:.625em}
293 ul.unstyled,ol.unstyled{margin-left:0}
294 ul.checklist{margin-left:.625em}
295 ul.checklist li>p:first-child>.fa-square-o:first-child,ul.checklist li>p:first-child>.fa-check-square-o:first-child{width:1.25em;font-size:.8em;position:relative;bottom:.125em}
296 ul.checklist li>p:first-child>input[type="checkbox"]:first-child{margin-right:.25em}
297 ul.inline{display:-ms-flexbox;display:-webkit-box;display:flex;-ms-flex-flow:row wrap;-webkit-flex-flow:row wrap;flex-flow:row wrap;list-style:none;margin:0 0 .625em -1.25em}
298 ul.inline>li{margin-left:1.25em}
299 .unstyled dl dt{font-weight:400;font-style:normal}
300 ol.arabic{list-style-type:decimal}
301 ol.decimal{list-style-type:decimal-leading-zero}
302 ol.loweralpha{list-style-type:lower-alpha}
303 ol.upperalpha{list-style-type:upper-alpha}
304 ol.lowerroman{list-style-type:lower-roman}
305 ol.upperroman{list-style-type:upper-roman}
306 ol.lowergreek{list-style-type:lower-greek}
307 .hdlist>table,.colist>table{border:0;background:none}
308 .hdlist>table>tbody>tr,.colist>table>tbody>tr{background:none}
309 td.hdlist1,td.hdlist2{vertical-align:top;padding:0 .625em}
310 td.hdlist1{font-weight:bold;padding-bottom:1.25em}
311 .literalblock+.colist,.listingblock+.colist{margin-top:-.5em}
312 .colist td:not([class]):first-child{padding:.4em .75em 0;line-height:1;vertical-align:top}
313 .colist td:not([class]):first-child img{max-width:none}
314 .colist td:not([class]):last-child{padding:.25em 0}
315 .thumb,.th{line-height:0;display:inline-block;border:solid 4px #fff;-webkit-box-shadow:0 0 0 1px #ddd;box-shadow:0 0 0 1px #ddd}
316 .imageblock.left{margin:.25em .625em 1.25em 0}
317 .imageblock.right{margin:.25em 0 1.25em .625em}
318 .imageblock>.title{margin-bottom:0}
319 .imageblock.thumb,.imageblock.th{border-width:6px}
320 .imageblock.thumb>.title,.imageblock.th>.title{padding:0 .125em}
321 .image.left,.image.right{margin-top:.25em;margin-bottom:.25em;display:inline-block;line-height:0}
322 .image.left{margin-right:.625em}
323 .image.right{margin-left:.625em}
324 a.image{text-decoration:none;display:inline-block}
325 a.image object{pointer-events:none}
326 sup.footnote,sup.footnoteref{font-size:.875em;position:static;vertical-align:super}
327 sup.footnote a,sup.footnoteref a{text-decoration:none}
328 sup.footnote a:active,sup.footnoteref a:active{text-decoration:underline}
329 #footnotes{padding-top:.75em;padding-bottom:.75em;margin-bottom:.625em}
330 #footnotes hr{width:20%;min-width:6.25em;margin:-.25em 0 .75em;border-width:1px 0 0}
331 #footnotes .footnote{padding:0 .375em 0 .225em;line-height:1.3334;font-size:.875em;margin-left:1.2em;margin-bottom:.2em}
332 #footnotes .footnote a:first-of-type{font-weight:bold;text-decoration:none;margin-left:-1.05em}
333 #footnotes .footnote:last-of-type{margin-bottom:0}
334 #content #footnotes{margin-top:-.625em;margin-bottom:0;padding:.75em 0}
335 .gist .file-data>table{border:0;background:#fff;width:100%;margin-bottom:0}
336 .gist .file-data>table td.line-data{width:99%}
337 div.unbreakable{page-break-inside:avoid}
338 .big{font-size:larger}
339 .small{font-size:smaller}
340 .underline{text-decoration:underline}
341 .overline{text-decoration:overline}
342 .line-through{text-decoration:line-through}
344 .aqua-background{background-color:#00fafa}
346 .black-background{background-color:#000}
348 .blue-background{background-color:#0000fa}
349 .fuchsia{color:#bf00bf}
350 .fuchsia-background{background-color:#fa00fa}
352 .gray-background{background-color:#7d7d7d}
353 .green{color:#006000}
354 .green-background{background-color:#007d00}
356 .lime-background{background-color:#00fa00}
357 .maroon{color:#600000}
358 .maroon-background{background-color:#7d0000}
360 .navy-background{background-color:#00007d}
361 .olive{color:#606000}
362 .olive-background{background-color:#7d7d00}
363 .purple{color:#600060}
364 .purple-background{background-color:#7d007d}
366 .red-background{background-color:#fa0000}
367 .silver{color:#909090}
368 .silver-background{background-color:#bcbcbc}
370 .teal-background{background-color:#007d7d}
371 .white{color:#bfbfbf}
372 .white-background{background-color:#fafafa}
373 .yellow{color:#bfbf00}
374 .yellow-background{background-color:#fafa00}
375 span.icon>.fa{cursor:default}
376 a span.icon>.fa{cursor:inherit}
377 .admonitionblock td.icon [class^="fa icon-"]{font-size:2.5em;text-shadow:1px 1px 2px rgba(0,0,0,.5);cursor:default}
378 .admonitionblock td.icon .icon-note::before{content:"\f05a";color:#19407c}
379 .admonitionblock td.icon .icon-tip::before{content:"\f0eb";text-shadow:1px 1px 2px rgba(155,155,0,.8);color:#111}
380 .admonitionblock td.icon .icon-warning::before{content:"\f071";color:#bf6900}
381 .admonitionblock td.icon .icon-caution::before{content:"\f06d";color:#bf3400}
382 .admonitionblock td.icon .icon-important::before{content:"\f06a";color:#bf0000}
383 .conum[data-value]{display:inline-block;color:#fff!important;background-color:rgba(0,0,0,.8);-webkit-border-radius:100px;border-radius:100px;text-align:center;font-size:.75em;width:1.67em;height:1.67em;line-height:1.67em;font-family:"Open Sans","DejaVu Sans",sans-serif;font-style:normal;font-weight:bold}
384 .conum[data-value] *{color:#fff!important}
385 .conum[data-value]+b{display:none}
386 .conum[data-value]::after{content:attr(data-value)}
387 pre .conum[data-value]{position:relative;top:-.125em}
388 b.conum *{color:inherit!important}
389 .conum:not([data-value]):empty{display:none}
390 dt,th.tableblock,td.content,div.footnote{text-rendering:optimizeLegibility}
391 h1,h2,p,td.content,span.alt{letter-spacing:-.01em}
392 p strong,td.content strong,div.footnote strong{letter-spacing:-.005em}
393 p,blockquote,dt,td.content,span.alt{font-size:1.0625rem}
394 p{margin-bottom:1.25rem}
395 .sidebarblock p,.sidebarblock dt,.sidebarblock td.content,p.tableblock{font-size:1em}
396 .exampleblock>.content{background-color:#fffef7;border-color:#e0e0dc;-webkit-box-shadow:0 1px 4px #e0e0dc;box-shadow:0 1px 4px #e0e0dc}
397 .print-only{display:none!important}
398 @page{margin:1.25cm .75cm}
399 @media print{*{-webkit-box-shadow:none!important;box-shadow:none!important;text-shadow:none!important}
401 a{color:inherit!important;text-decoration:underline!important}
402 a.bare,a[href^="#"],a[href^="mailto:"]{text-decoration:none!important}
403 a[href^="http:"]:not(.bare)::after,a[href^="https:"]:not(.bare)::after{content:"(" attr(href) ")";display:inline-block;font-size:.875em;padding-left:.25em}
404 abbr[title]::after{content:" (" attr(title) ")"}
405 pre,blockquote,tr,img,object,svg{page-break-inside:avoid}
406 thead{display:table-header-group}
408 p,blockquote,dt,td.content{font-size:1em;orphans:3;widows:3}
409 h2,h3,#toctitle,.sidebarblock>.content>.title{page-break-after:avoid}
410 #toc,.sidebarblock,.exampleblock>.content{background:none!important}
411 #toc{border-bottom:1px solid #dddddf!important;padding-bottom:0!important}
412 body.book #header{text-align:center}
413 body.book #header>h1:first-child{border:0!important;margin:2.5em 0 1em}
414 body.book #header .details{border:0!important;display:block;padding:0!important}
415 body.book #header .details span:first-child{margin-left:0!important}
416 body.book #header .details br{display:block}
417 body.book #header .details br+span::before{content:none!important}
418 body.book #toc{border:0!important;text-align:left!important;padding:0!important;margin:0!important}
419 body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-break-before:always}
420 .listingblock code[data-lang]::before{display:block}
421 #footer{padding:0 .9375em}
422 .hide-on-print{display:none!important}
423 .print-only{display:block!important}
424 .hide-for-print{display:none!important}
425 .show-for-print{display:inherit!important}}
426 @media print,amzn-kf8{#header>h1:first-child{margin-top:1.25rem}
427 .sect1{padding:0!important}
428 .sect1+.sect1{border:0}
429 #footer{background:none}
430 #footer-text{color:rgba(0,0,0,.6);font-size:.9em}}
431 @media amzn-kf8{#header,#content,#footnotes,#footer{padding:0}}
434 <body class="article toc2 toc-left">
436 <h1>Boost.SmartPtr: The Smart Pointer Library</h1>
437 <div class="details">
438 <span id="author" class="author">Greg Colvin, Beman Dawes, Peter Dimov, Glen Fernandes</span><br>
440 <div id="toc" class="toc2">
441 <div id="toctitle">Table of Contents</div>
442 <ul class="sectlevel1">
443 <li><a href="#introduction">Introduction</a></li>
444 <li><a href="#changelog">Revision History</a>
445 <ul class="sectlevel2">
446 <li><a href="#changelog_changes_in_1_72_0">Changes in 1.72.0</a></li>
447 <li><a href="#changelog_changes_in_1_71_0">Changes in 1.71.0</a></li>
448 <li><a href="#changelog_changes_in_1_65_0">Changes in 1.65.0</a></li>
451 <li><a href="#scoped_ptr">scoped_ptr: Scoped Object Ownership</a>
452 <ul class="sectlevel2">
453 <li><a href="#scoped_ptr_description">Description</a></li>
454 <li><a href="#scoped_ptr_synopsis">Synopsis</a></li>
455 <li><a href="#scoped_ptr_members">Members</a></li>
456 <li><a href="#scoped_ptr_free_functions">Free Functions</a></li>
457 <li><a href="#scoped_ptr_example">Example</a></li>
458 <li><a href="#scoped_ptr_rationale">Rationale</a></li>
459 <li><a href="#scoped_ptr_handlebody_idiom">Handle/Body Idiom</a></li>
460 <li><a href="#scoped_ptr_frequently_asked_questions">Frequently Asked Questions</a></li>
463 <li><a href="#scoped_array">scoped_array: Scoped Array Ownership</a>
464 <ul class="sectlevel2">
465 <li><a href="#scoped_array_description">Description</a></li>
466 <li><a href="#scoped_array_synopsis">Synopsis</a></li>
467 <li><a href="#scoped_array_members">Members</a></li>
468 <li><a href="#scoped_array_free_functions">Free Functions</a></li>
471 <li><a href="#shared_ptr">shared_ptr: Shared Ownership</a>
472 <ul class="sectlevel2">
473 <li><a href="#shared_ptr_description">Description</a></li>
474 <li><a href="#shared_ptr_best_practices">Best Practices</a></li>
475 <li><a href="#shared_ptr_synopsis">Synopsis</a></li>
476 <li><a href="#shared_ptr_members">Members</a></li>
477 <li><a href="#shared_ptr_free_functions">Free Functions</a></li>
478 <li><a href="#shared_ptr_example">Example</a></li>
479 <li><a href="#shared_ptr_handlebody_idiom">Handle/Body Idiom</a></li>
480 <li><a href="#shared_ptr_thread_safety">Thread Safety</a></li>
481 <li><a href="#shared_ptr_frequently_asked_questions">Frequently Asked Questions</a></li>
484 <li><a href="#weak_ptr">weak_ptr: Non-owning Observer</a>
485 <ul class="sectlevel2">
486 <li><a href="#weak_ptr_description">Description</a></li>
487 <li><a href="#weak_ptr_synopsis">Synopsis</a></li>
488 <li><a href="#weak_ptr_members">Members</a></li>
489 <li><a href="#weak_ptr_free_functions">Free Functions</a></li>
490 <li><a href="#weak_ptr_frequently_asked_questions">Frequently Asked Questions</a></li>
493 <li><a href="#make_shared">make_shared: Creating shared_ptr</a>
494 <ul class="sectlevel2">
495 <li><a href="#make_shared_description">Description</a></li>
496 <li><a href="#make_shared_rationale">Rationale</a></li>
497 <li><a href="#make_shared_synopsis">Synopsis</a></li>
498 <li><a href="#make_shared_common_requirements">Common Requirements</a></li>
499 <li><a href="#make_shared_free_functions">Free Functions</a></li>
502 <li><a href="#enable_shared_from_this">enable_shared_from_this</a>
503 <ul class="sectlevel2">
504 <li><a href="#enable_shared_from_this_description">Description</a></li>
505 <li><a href="#enable_shared_from_this_example">Example</a></li>
506 <li><a href="#enable_shared_from_this_synopsis">Synopsis</a></li>
507 <li><a href="#enable_shared_from_this_members">Members</a></li>
510 <li><a href="#enable_shared_from">enable_shared_from</a>
511 <ul class="sectlevel2">
512 <li><a href="#enable_shared_from_description">Description</a></li>
513 <li><a href="#enable_shared_from_example">Example</a></li>
514 <li><a href="#enable_shared_from_synopsis">Synopsis</a></li>
515 <li><a href="#enable_shared_from_functions">Functions</a></li>
518 <li><a href="#make_unique">make_unique: Creating unique_ptr</a>
519 <ul class="sectlevel2">
520 <li><a href="#make_unique_description">Description</a></li>
521 <li><a href="#make_unique_rationale">Rationale</a></li>
522 <li><a href="#make_unique_synopsis">Synopsis</a></li>
523 <li><a href="#make_unique_free_functions">Free Functions</a></li>
526 <li><a href="#allocate_unique">allocate_unique: Creating unique_ptr</a>
527 <ul class="sectlevel2">
528 <li><a href="#allocate_unique_description">Description</a></li>
529 <li><a href="#allocate_unique_rationale">Rationale</a></li>
530 <li><a href="#allocate_unique_synopsis">Synopsis</a></li>
531 <li><a href="#allocate_unique_common_requirements">Common Requirements</a></li>
532 <li><a href="#allocate_unique_free_functions">Free Functions</a></li>
533 <li><a href="#allocate_unique_deleter">Deleter</a></li>
536 <li><a href="#intrusive_ptr">intrusive_ptr: Managing Objects with Embedded Counts</a>
537 <ul class="sectlevel2">
538 <li><a href="#intrusive_ptr_description">Description</a></li>
539 <li><a href="#intrusive_ptr_synopsis">Synopsis</a></li>
540 <li><a href="#intrusive_ptr_members">Members</a></li>
541 <li><a href="#intrusive_ptr_free_functions">Free Functions</a></li>
544 <li><a href="#intrusive_ref_counter">intrusive_ref_counter</a>
545 <ul class="sectlevel2">
546 <li><a href="#intrusive_ref_counter_description">Description</a></li>
547 <li><a href="#intrusive_ref_counter_synopsis">Synopsis</a></li>
548 <li><a href="#intrusive_ref_counter_members">Members</a></li>
549 <li><a href="#intrusive_ref_counter_free_functions">Free Functions</a></li>
552 <li><a href="#local_shared_ptr">local_shared_ptr: Shared Ownership within a Single Thread</a>
553 <ul class="sectlevel2">
554 <li><a href="#local_shared_ptr_description">Description</a></li>
555 <li><a href="#local_shared_ptr_synopsis">Synopsis</a></li>
556 <li><a href="#local_shared_ptr_members">Members</a></li>
557 <li><a href="#local_shared_ptr_free_functions">Free Functions</a></li>
560 <li><a href="#make_local_shared">make_local_shared: Creating local_shared_ptr</a>
561 <ul class="sectlevel2">
562 <li><a href="#make_local_shared_description">Description</a></li>
563 <li><a href="#make_local_shared_synopsis">Synopsis</a></li>
564 <li><a href="#make_local_shared_description_2">Description</a></li>
567 <li><a href="#pointer_cast">Generic Pointer Casts</a>
568 <ul class="sectlevel2">
569 <li><a href="#pointer_cast_description">Description</a></li>
570 <li><a href="#pointer_cast_rationale">Rationale</a></li>
571 <li><a href="#pointer_cast_synopsis">Synopsis</a></li>
572 <li><a href="#pointer_cast_free_functions">Free Functions</a></li>
573 <li><a href="#pointer_cast_example">Example</a></li>
576 <li><a href="#pointer_to_other">pointer_to_other</a>
577 <ul class="sectlevel2">
578 <li><a href="#pointer_to_other_description">Description</a></li>
579 <li><a href="#pointer_to_other_rationale">Rationale</a></li>
580 <li><a href="#pointer_to_other_synopsis">Synopsis</a></li>
581 <li><a href="#pointer_to_other_example">Example</a></li>
584 <li><a href="#atomic_shared_ptr">atomic_shared_ptr</a>
585 <ul class="sectlevel2">
586 <li><a href="#atomic_shared_ptr_description">Description</a></li>
587 <li><a href="#atomic_shared_ptr_synopsis">Synopsis</a></li>
588 <li><a href="#atomic_shared_ptr_members">Members</a></li>
591 <li><a href="#techniques">Appendix A: Smart Pointer Programming Techniques</a>
592 <ul class="sectlevel2">
593 <li><a href="#techniques_incomplete">Using incomplete classes for implementation hiding</a></li>
594 <li><a href="#techniques_the_pimpl_idiom">The "Pimpl" idiom</a></li>
595 <li><a href="#techniques_using_abstract_classes_for_implementation_hiding">Using abstract classes for implementation hiding</a></li>
596 <li><a href="#techniques_preventing_delete_px_get">Preventing <code>delete px.get()</code></a></li>
597 <li><a href="#techniques_encapsulating_allocation_details_wrapping_factory_functions">Encapsulating allocation details, wrapping factory functions</a></li>
598 <li><a href="#techniques_static">Using a shared_ptr to hold a pointer to a statically allocated object</a></li>
599 <li><a href="#techniques_using_a_shared_ptr_to_hold_a_pointer_to_a_com_object">Using a shared_ptr to hold a pointer to a COM Object</a></li>
600 <li><a href="#techniques_intrusive">Using a shared_ptr to hold a pointer to an object with an embedded reference count</a></li>
601 <li><a href="#techniques_using_a_shared_ptr_to_hold_another_shared_ownership_smart_pointer">Using a shared_ptr to hold another shared ownership smart pointer</a></li>
602 <li><a href="#techniques_from_raw">Obtaining a shared_ptr from a raw pointer</a></li>
603 <li><a href="#techniques_obtaining_a_shared_ptr_weak_ptr_to_this_in_a_constructor">Obtaining a shared_ptr (weak_ptr) to this in a constructor</a></li>
604 <li><a href="#techniques_obtaining_a_shared_ptr_to_this">Obtaining a shared_ptr to this</a></li>
605 <li><a href="#techniques_using_shared_ptr_as_a_smart_counted_handle">Using shared_ptr as a smart counted handle</a></li>
606 <li><a href="#techniques_using_shared_ptr_to_execute_code_on_block_exit">Using shared_ptr to execute code on block exit</a></li>
607 <li><a href="#techniques_using_shared_ptrvoid_to_hold_an_arbitrary_object">Using shared_ptr<void> to hold an arbitrary object</a></li>
608 <li><a href="#techniques_associating_arbitrary_data_with_heterogeneous_shared_ptr_instances">Associating arbitrary data with heterogeneous <code>shared_ptr</code> instances</a></li>
609 <li><a href="#techniques_using_shared_ptr_as_a_copyconstructible_mutex_lock">Using <code>shared_ptr</code> as a <code>CopyConstructible</code> mutex lock</a></li>
610 <li><a href="#techniques_using_shared_ptr_to_wrap_member_function_calls">Using shared_ptr to wrap member function calls</a></li>
611 <li><a href="#techniques_delayed_deallocation">Delayed deallocation</a></li>
612 <li><a href="#techniques_weak_without_shared">Weak pointers to objects not managed by a shared_ptr</a></li>
615 <li><a href="#history">Appendix B: History and Acknowledgments</a>
616 <ul class="sectlevel2">
617 <li><a href="#history_summer_1994">Summer 1994</a></li>
618 <li><a href="#history_october_1998">October 1998</a></li>
619 <li><a href="#history_may_1999">May 1999</a></li>
620 <li><a href="#history_september_1999">September 1999</a></li>
621 <li><a href="#history_november_1999">November 1999</a></li>
622 <li><a href="#history_may_2001">May 2001</a></li>
623 <li><a href="#history_january_2002">January 2002</a></li>
624 <li><a href="#history_march_2003">March 2003</a></li>
625 <li><a href="#history_july_2007">July 2007</a></li>
626 <li><a href="#history_november_2012">November 2012</a></li>
627 <li><a href="#history_april_2013">April 2013</a></li>
628 <li><a href="#history_february_2014">February 2014</a></li>
629 <li><a href="#history_february_2017">February 2017</a></li>
630 <li><a href="#history_june_2017">June 2017</a></li>
631 <li><a href="#history_august_2019">August 2019</a></li>
634 <li><a href="#shared_array">Appendix C: shared_array (deprecated)</a>
635 <ul class="sectlevel2">
636 <li><a href="#shared_array_description">Description</a></li>
637 <li><a href="#shared_array_synopsis">Synopsis</a></li>
638 <li><a href="#shared_array_members">Members</a></li>
639 <li><a href="#shared_array_free_functions">Free Functions</a></li>
642 <li><a href="#copyright">Appendix D: Copyright and License</a></li>
648 <h2 id="introduction">Introduction</h2>
649 <div class="sectionbody">
650 <div class="paragraph">
651 <p>Smart pointers are objects which store pointers to dynamically allocated (heap) objects.
652 They behave much like built-in C++ pointers except that they automatically delete the object
653 pointed to at the appropriate time. Smart pointers are particularly useful in the face of
654 exceptions as they ensure proper destruction of dynamically allocated objects. They can also be
655 used to keep track of dynamically allocated objects shared by multiple owners.</p>
657 <div class="paragraph">
658 <p>Conceptually, smart pointers are seen as owning the object pointed to, and thus responsible for
659 deletion of the object when it is no longer needed. As such, they are examples of the "resource
660 acquisition is initialization" idiom described in Bjarne Stroustrup’s "The C++ Programming Language",
661 3rd edition, Section 14.4, Resource Management.</p>
663 <div class="paragraph">
664 <p>This library provides six smart pointer class templates:</p>
669 <p><code><a href="#scoped_ptr">scoped_ptr</a></code>, used to contain ownership of a dynamically allocated object to the current scope;</p>
672 <p><code><a href="#scoped_array">scoped_array</a></code>, which provides scoped ownership for a dynamically allocated array;</p>
675 <p><code><a href="#shared_ptr">shared_ptr</a></code>, a versatile tool for managing shared ownership of an object or array;</p>
678 <p><code><a href="#weak_ptr">weak_ptr</a></code>, a non-owning observer to a shared_ptr-managed object that can be promoted temporarily to shared_ptr;</p>
681 <p><code><a href="#intrusive_ptr">intrusive_ptr</a></code>, a pointer to objects with an embedded reference count;</p>
684 <p><code><a href="#local_shared_ptr">local_shared_ptr</a></code>, providing shared ownership within a single thread.</p>
688 <div class="paragraph">
689 <p><code>shared_ptr</code> and <code>weak_ptr</code> are part of the C++ standard since its 2011 iteration.</p>
691 <div class="paragraph">
692 <p>In addition, the library contains the following supporting utility functions and classes:</p>
697 <p><code><a href="#make_shared">make_shared</a></code>, a factory function for creating objects that returns a <code>shared_ptr</code>;</p>
700 <p><code><a href="#make_unique">make_unique</a></code>, a factory function returning <code>std::unique_ptr</code>;</p>
703 <p><code><a href="#allocate_unique">allocate_unique</a></code>, a factory function for creating objects using an allocator that returns a <code>std::unique_ptr</code>;</p>
706 <p><code><a href="#enable_shared_from_this">enable_shared_from_this</a></code>, a helper base class that enables the acquisition of a <code>shared_ptr</code> pointing to <code>this</code>;</p>
709 <p><code><a href="#pointer_to_other">pointer_to_other</a></code>, a helper trait for converting one smart pointer type to another;</p>
712 <p><code><a href="#pointer_cast">static_pointer_cast</a></code> and companions, generic smart pointer casts;</p>
715 <p><code><a href="#intrusive_ref_counter">intrusive_ref_counter</a></code>, a helper base class containing a reference count.</p>
718 <p><code><a href="#atomic_shared_ptr">atomic_shared_ptr</a></code>, a helper class implementing the interface of <code>std::atomic</code> for a value of type <code>shared_ptr</code>.</p>
722 <div class="paragraph">
723 <p>As a general rule, the destructor or <code>operator delete</code> for an object managed by pointers in the library
724 are not allowed to throw exceptions.</p>
729 <h2 id="changelog">Revision History</h2>
730 <div class="sectionbody">
732 <h3 id="changelog_changes_in_1_72_0">Changes in 1.72.0</h3>
736 <p>Added <code>allocate_unique</code></p>
742 <h3 id="changelog_changes_in_1_71_0">Changes in 1.71.0</h3>
746 <p>Added aliasing constructors to <code>weak_ptr</code></p>
749 <p>Added <code>weak_ptr<T>::empty()</code></p>
752 <p>Added <code>enable_shared_from</code>, <code>shared_from</code>, and <code>weak_from</code></p>
758 <h3 id="changelog_changes_in_1_65_0">Changes in 1.65.0</h3>
762 <p>Added <code>atomic_shared_ptr</code></p>
765 <p>Added <code>local_shared_ptr</code>, <code>make_local_shared</code></p>
773 <h2 id="scoped_ptr">scoped_ptr: Scoped Object Ownership</h2>
774 <div class="sectionbody">
776 <h3 id="scoped_ptr_description">Description</h3>
777 <div class="paragraph">
778 <p>The <code>scoped_ptr</code> class template stores a pointer to a dynamically allocated object.
779 (Dynamically allocated objects are allocated with the C++ <code>new</code> expression.) The
780 object pointed to is guaranteed to be deleted, either on destruction of the <code>scoped_ptr</code>,
781 or via an explicit <code>reset</code>. See the <a href="#scoped_ptr_example">example</a>.</p>
783 <div class="paragraph">
784 <p><code>scoped_ptr</code> is a simple solution for simple needs. It supplies a basic "resource acquisition
785 is initialization" facility, without shared-ownership or transfer-of-ownership semantics.
786 Both its name and enforcement of semantics (by being noncopyable) signal its intent to retain
787 ownership solely within the current scope. Because it is noncopyable, it is safer than <code>shared_ptr</code>
788 for pointers which should not be copied.</p>
790 <div class="paragraph">
791 <p>Because <code>scoped_ptr</code> is simple, in its usual implementation every operation is as fast as for a
792 built-in pointer and it has no more space overhead that a built-in pointer.</p>
794 <div class="paragraph">
795 <p><code>scoped_ptr</code> cannot be used in C++ Standard Library containers. Use <code>shared_ptr</code> or <code>std::unique_ptr</code>
796 if you need a smart pointer that can.</p>
798 <div class="paragraph">
799 <p><code>scoped_ptr</code> cannot correctly hold a pointer to a dynamically allocated array. See <code>scoped_array</code> for that usage.</p>
801 <div class="paragraph">
802 <p>The class template is parameterized on <code>T</code>, the type of the object pointed to. Destroying <code>T</code> must not thow exceptions,
803 and <code>T</code> must be complete at the point <code>scoped_ptr<T>::~scoped_ptr</code> is instantiated.</p>
807 <h3 id="scoped_ptr_synopsis">Synopsis</h3>
808 <div class="paragraph">
809 <p><code>scoped_ptr</code> is defined in <code><boost/smart_ptr/scoped_ptr.hpp></code>.</p>
811 <div class="listingblock">
812 <div class="content">
813 <pre class="highlight"><code>namespace boost {
815 template<class T> class scoped_ptr {
818 scoped_ptr(scoped_ptr const&);
819 scoped_ptr& operator=(scoped_ptr const&);
821 void operator==(scoped_ptr const&) const;
822 void operator!=(scoped_ptr const&) const;
826 typedef T element_type;
828 explicit scoped_ptr(T * p = 0) noexcept;
829 ~scoped_ptr() noexcept;
831 void reset(T * p = 0) noexcept;
833 T & operator*() const noexcept;
834 T * operator->() const noexcept;
835 T * get() const noexcept;
837 explicit operator bool() const noexcept;
839 void swap(scoped_ptr & b) noexcept;
842 template<class T> void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) noexcept;
844 template<class T>
845 bool operator==( scoped_ptr<T> const & p, std::nullptr_t ) noexcept;
846 template<class T>
847 bool operator==( std::nullptr_t, scoped_ptr<T> const & p ) noexcept;
849 template<class T>
850 bool operator!=( scoped_ptr<T> const & p, std::nullptr_t ) noexcept;
851 template<class T>
852 bool operator!=( std::nullptr_t, scoped_ptr<T> const & p ) noexcept;
858 <h3 id="scoped_ptr_members">Members</h3>
860 <h4 id="scoped_ptr_element_type">element_type</h4>
861 <div class="literalblock">
862 <div class="content">
863 <pre>typedef T element_type;</pre>
866 <div class="paragraph">
867 <p>Provides the type of the stored pointer.</p>
871 <h4 id="scoped_ptr_constructor">constructor</h4>
872 <div class="literalblock">
873 <div class="content">
874 <pre>explicit scoped_ptr(T * p = 0) noexcept;</pre>
877 <div class="paragraph">
878 <p>Constructs a <code>scoped_ptr</code>, storing a copy of <code>p</code>, which must have been allocated via a
879 C++ <code>new</code> expression or be 0. <code>T</code> is not required be a complete type.</p>
883 <h4 id="scoped_ptr_destructor">destructor</h4>
884 <div class="literalblock">
885 <div class="content">
886 <pre>~scoped_ptr() noexcept;</pre>
889 <div class="paragraph">
890 <p>Destroys the object pointed to by the stored pointer, if any, as if by using
891 <code>delete this->get()</code>. <code>T</code> must be a complete type.</p>
895 <h4 id="scoped_ptr_reset">reset</h4>
896 <div class="literalblock">
897 <div class="content">
898 <pre>void reset(T * p = 0) noexcept;</pre>
901 <div class="paragraph">
902 <p>Deletes the object pointed to by the stored pointer and then stores a copy of
903 <code>p</code>, which must have been allocated via a C++ <code>new</code> expression or be 0.</p>
905 <div class="paragraph">
906 <p>Since the previous object needs to be deleted, <code>T</code> must be a complete type.</p>
910 <h4 id="scoped_ptr_indirection">indirection</h4>
911 <div class="literalblock">
912 <div class="content">
913 <pre>T & operator*() const noexcept;</pre>
916 <div class="paragraph">
917 <p>Returns a reference to the object pointed to by the stored pointer. Behavior is undefined if the stored pointer is 0.</p>
919 <div class="literalblock">
920 <div class="content">
921 <pre>T * operator->() const noexcept;</pre>
924 <div class="paragraph">
925 <p>Returns the stored pointer. Behavior is undefined if the stored pointer is 0.</p>
929 <h4 id="scoped_ptr_get">get</h4>
930 <div class="literalblock">
931 <div class="content">
932 <pre>T * get() const noexcept;</pre>
935 <div class="paragraph">
936 <p>Returns the stored pointer. <code>T</code> need not be a complete type.</p>
940 <h4 id="scoped_ptr_conversions">conversions</h4>
941 <div class="literalblock">
942 <div class="content">
943 <pre>explicit operator bool () const noexcept; // never throws</pre>
946 <div class="paragraph">
947 <p>Returns <code>get() != 0</code>.</p>
949 <div class="admonitionblock note">
953 <div class="title">Note</div>
956 On C++03 compilers, the return value is of an unspecified type.
963 <h4 id="scoped_ptr_swap">swap</h4>
964 <div class="literalblock">
965 <div class="content">
966 <pre>void swap(scoped_ptr & b) noexcept;</pre>
969 <div class="paragraph">
970 <p>Exchanges the contents of the two smart pointers. <code>T</code> need not be a complete type.</p>
975 <h3 id="scoped_ptr_free_functions">Free Functions</h3>
977 <h4 id="scoped_ptr_swap_2">swap</h4>
978 <div class="literalblock">
979 <div class="content">
980 <pre>template<class T> void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) noexcept;</pre>
983 <div class="paragraph">
984 <p>Equivalent to <code>a.swap(b)</code>.</p>
988 <h4 id="scoped_ptr_comparisons">comparisons</h4>
989 <div class="literalblock">
990 <div class="content">
991 <pre>template<class T> bool operator==( scoped_ptr<T> const & p, std::nullptr_t ) noexcept;</pre>
994 <div class="literalblock">
995 <div class="content">
996 <pre>template<class T> bool operator==( std::nullptr_t, scoped_ptr<T> const & p ) noexcept;</pre>
999 <div class="paragraph">
1000 <p>Returns <code>p.get() == nullptr</code>.</p>
1002 <div class="literalblock">
1003 <div class="content">
1004 <pre>template<class T> bool operator!=( scoped_ptr<T> const & p, std::nullptr_t ) noexcept;</pre>
1007 <div class="literalblock">
1008 <div class="content">
1009 <pre>template<class T> bool operator!=( std::nullptr_t, scoped_ptr<T> const & p ) noexcept;</pre>
1012 <div class="paragraph">
1013 <p>Returns <code>p.get() != nullptr</code>.</p>
1018 <h3 id="scoped_ptr_example">Example</h3>
1019 <div class="paragraph">
1020 <p>Here’s an example that uses <code>scoped_ptr</code>.</p>
1022 <div class="listingblock">
1023 <div class="content">
1024 <pre class="highlight"><code>#include <boost/scoped_ptr.hpp>
1025 #include <iostream>
1027 struct Shoe { ~Shoe() { std::cout << "Buckle my shoe\n"; } };
1030 boost::scoped_ptr<int> ptr;
1032 MyClass() : ptr(new int) { *ptr = 0; }
1033 int add_one() { return ++*ptr; }
1038 boost::scoped_ptr<Shoe> x(new Shoe);
1039 MyClass my_instance;
1040 std::cout << my_instance.add_one() << '\n';
1041 std::cout << my_instance.add_one() << '\n';
1045 <div class="paragraph">
1046 <p>The example program produces the beginning of a child’s nursery rhyme:</p>
1048 <div class="listingblock">
1049 <div class="content">
1050 <pre class="highlight"><code>1
1052 Buckle my shoe</code></pre>
1057 <h3 id="scoped_ptr_rationale">Rationale</h3>
1058 <div class="paragraph">
1059 <p>The primary reason to use <code>scoped_ptr</code> rather than <code>std::auto_ptr</code> or <code>std::unique_ptr</code> is to let readers of your code
1060 know that you intend "resource acquisition is initialization" to be applied only for the current scope, and have no intent to transfer ownership.</p>
1062 <div class="paragraph">
1063 <p>A secondary reason to use <code>scoped_ptr</code> is to prevent a later maintenance programmer from adding a function that transfers
1064 ownership by returning the <code>auto_ptr</code>, because the maintenance programmer saw <code>auto_ptr</code>, and assumed ownership could safely be transferred.</p>
1066 <div class="paragraph">
1067 <p>Think of <code>bool</code> vs <code>int</code>. We all know that under the covers <code>bool</code> is usually just an <code>int</code>. Indeed, some argued against including bool in the C++
1068 standard because of that. But by coding <code>bool</code> rather than <code>int</code>, you tell your readers what your intent is. Same with <code>scoped_ptr</code>; by using it you are signaling intent.</p>
1070 <div class="paragraph">
1071 <p>It has been suggested that <code>scoped_ptr<T></code> is equivalent to <code>std::auto_ptr<T> const</code>. Ed Brey pointed out, however, that <code>reset</code> will not work on a <code>std::auto_ptr<T> const</code>.</p>
1075 <h3 id="scoped_ptr_handlebody_idiom">Handle/Body Idiom</h3>
1076 <div class="paragraph">
1077 <p>One common usage of <code>scoped_ptr</code> is to implement a handle/body (also called pimpl) idiom which avoids exposing the body (implementation) in the header file.</p>
1079 <div class="paragraph">
1080 <p>The <code><a href="../../example/scoped_ptr_example_test.cpp">scoped_ptr_example_test.cpp</a></code> sample program includes a header file,
1081 <code><a href="../../example/scoped_ptr_example.hpp">scoped_ptr_example.hpp</a></code>, which uses a <code>scoped_ptr<></code> to an incomplete type to hide the
1082 implementation. The instantiation of member functions which require a complete type occurs in the <code><a href="../../example/scoped_ptr_example.cpp">scoped_ptr_example.cpp</a></code>
1083 implementation file.</p>
1087 <h3 id="scoped_ptr_frequently_asked_questions">Frequently Asked Questions</h3>
1088 <div class="qlist qanda">
1091 <p><em>Why doesn’t <code>scoped_ptr</code> have a <code>release()</code> member?</em></p>
1092 <p>When reading source code, it is valuable to be able to draw conclusions about program behavior based on the types being used. If <code>scoped_ptr</code> had a <code>release()</code> member,
1093 it would become possible to transfer ownership of the held pointer, weakening its role as a way of limiting resource lifetime to a given context. Use <code>std::auto_ptr</code> where
1094 transfer of ownership is required. (supplied by Dave Abrahams)</p>
1102 <h2 id="scoped_array">scoped_array: Scoped Array Ownership</h2>
1103 <div class="sectionbody">
1105 <h3 id="scoped_array_description">Description</h3>
1106 <div class="paragraph">
1107 <p>The <code>scoped_array</code> class template stores a pointer to a dynamically allocated array.
1108 (Dynamically allocated arrays are allocated with the C++ <code>new[]</code> expression.) The array
1109 pointed to is guaranteed to be deleted, either on destruction of the <code>scoped_array</code>,
1110 or via an explicit <code>reset</code>.</p>
1112 <div class="paragraph">
1113 <p>The <code>scoped_array</code> template is a simple solution for simple needs. It supplies a basic
1114 "resource acquisition is initialization" facility, without shared-ownership or
1115 transfer-of-ownership semantics. Both its name and enforcement of semantics
1116 (by being noncopyable) signal its intent to retain ownership solely within the current scope.
1117 Because it is noncopyable, it is safer than <code>shared_ptr<T[]></code> for pointers which should not be copied.</p>
1119 <div class="paragraph">
1120 <p>Because <code>scoped_array</code> is so simple, in its usual implementation every operation is as fast as a
1121 built-in array pointer and it has no more space overhead that a built-in array pointer.</p>
1123 <div class="paragraph">
1124 <p>It cannot be used in C++ standard library containers. See <code>shared_ptr<T[]></code> if <code>scoped_array</code>
1125 does not meet your needs.</p>
1127 <div class="paragraph">
1128 <p>It cannot correctly hold a pointer to a single object. See <code>scoped_ptr</code> for that usage.</p>
1130 <div class="paragraph">
1131 <p><code>std::vector</code> is an alternative to <code>scoped_array</code> that is a bit heavier duty but far more flexible.
1132 <code>boost::array</code> is an alternative that does not use dynamic allocation.</p>
1134 <div class="paragraph">
1135 <p>The class template is parameterized on <code>T</code>, the type of the object pointed to.</p>
1139 <h3 id="scoped_array_synopsis">Synopsis</h3>
1140 <div class="paragraph">
1141 <p><code>scoped_array</code> is defined in <code><boost/smart_ptr/scoped_array.hpp></code>.</p>
1143 <div class="listingblock">
1144 <div class="content">
1145 <pre class="highlight"><code>namespace boost {
1147 template<class T> class scoped_array {
1150 scoped_array(scoped_array const &);
1151 scoped_array & operator=(scoped_array const &);
1153 void operator==( scoped_array const& ) const;
1154 void operator!=( scoped_array const& ) const;
1158 typedef T element_type;
1160 explicit scoped_array(T * p = 0) noexcept;
1161 ~scoped_array() noexcept;
1163 void reset(T * p = 0) noexcept;
1165 T & operator[](std::ptrdiff_t i) const noexcept;
1166 T * get() const noexcept;
1168 explicit operator bool () const noexcept;
1170 void swap(scoped_array & b) noexcept;
1173 template<class T> void swap(scoped_array<T> & a, scoped_array<T> & b) noexcept;
1175 template<class T>
1176 bool operator==( scoped_array<T> const & p, std::nullptr_t ) noexcept;
1177 template<class T>
1178 bool operator==( std::nullptr_t, scoped_array<T> const & p ) noexcept;
1180 template<class T>
1181 bool operator!=( scoped_array<T> const & p, std::nullptr_t ) noexcept;
1182 template<class T>
1183 bool operator!=( std::nullptr_t, scoped_array<T> const & p ) noexcept;
1189 <h3 id="scoped_array_members">Members</h3>
1191 <h4 id="scoped_array_element_type">element_type</h4>
1192 <div class="literalblock">
1193 <div class="content">
1194 <pre>typedef T element_type;</pre>
1197 <div class="paragraph">
1198 <p>Provides the type of the stored pointer.</p>
1202 <h4 id="scoped_array_constructors">constructors</h4>
1203 <div class="literalblock">
1204 <div class="content">
1205 <pre>explicit scoped_array(T * p = 0) noexcept;</pre>
1208 <div class="paragraph">
1209 <p>Constructs a <code>scoped_array</code>, storing a copy of <code>p</code>, which must have been
1210 allocated via a C++ <code>new[]</code> expression or be 0. <code>T</code> is not required be a complete type.</p>
1214 <h4 id="scoped_array_destructor">destructor</h4>
1215 <div class="literalblock">
1216 <div class="content">
1217 <pre>~scoped_array() noexcept;</pre>
1220 <div class="paragraph">
1221 <p>Deletes the array pointed to by the stored pointer. Note that <code>delete[]</code> on a pointer with
1222 a value of 0 is harmless. <code>T</code> must be complete, and <code>delete[]</code> on the stored pointer must
1223 not throw exceptions.</p>
1227 <h4 id="scoped_array_reset">reset</h4>
1228 <div class="literalblock">
1229 <div class="content">
1230 <pre>void reset(T * p = 0) noexcept;</pre>
1233 <div class="paragraph">
1234 <p>Deletes the array pointed to by the stored pointer and then stores a copy of <code>p</code>,
1235 which must have been allocated via a C++ <code>new[]</code> expression or be 0. <code>T</code> must be complete,
1236 and <code>delete[]</code> on the stored pointer must not throw exceptions.</p>
1240 <h4 id="scoped_array_subscripting">subscripting</h4>
1241 <div class="literalblock">
1242 <div class="content">
1243 <pre>T & operator[](std::ptrdiff_t i) const noexcept;</pre>
1246 <div class="paragraph">
1247 <p>Returns a reference to element <code>i</code> of the array pointed to by the stored pointer.
1248 Behavior is undefined and almost certainly undesirable if the stored pointer is 0,
1249 or if <code>i</code> is less than 0 or is greater than or equal to the number of elements in
1254 <h4 id="scoped_array_get">get</h4>
1255 <div class="literalblock">
1256 <div class="content">
1257 <pre>T * get() const noexcept;</pre>
1260 <div class="paragraph">
1261 <p>Returns the stored pointer. <code>T</code> need not be a complete type.</p>
1265 <h4 id="scoped_array_conversions">conversions</h4>
1266 <div class="literalblock">
1267 <div class="content">
1268 <pre>explicit operator bool () const noexcept;</pre>
1271 <div class="paragraph">
1272 <p>Returns <code>get() != 0</code>.</p>
1274 <div class="admonitionblock note">
1278 <div class="title">Note</div>
1280 <td class="content">
1281 On C++03 compilers, the return value is of an unspecified type.
1288 <h4 id="scoped_array_swap">swap</h4>
1289 <div class="literalblock">
1290 <div class="content">
1291 <pre>void swap(scoped_array & b) noexcept;</pre>
1294 <div class="paragraph">
1295 <p>Exchanges the contents of the two smart pointers. <code>T</code> need not be a complete type.</p>
1300 <h3 id="scoped_array_free_functions">Free Functions</h3>
1302 <h4 id="scoped_array_swap_2">swap</h4>
1303 <div class="literalblock">
1304 <div class="content">
1305 <pre>template<class T> void swap(scoped_array<T> & a, scoped_array<T> & b) noexcept;</pre>
1308 <div class="paragraph">
1309 <p>Equivalent to <code>a.swap(b)</code>.</p>
1313 <h4 id="scoped_array_comparisons">comparisons</h4>
1314 <div class="literalblock">
1315 <div class="content">
1316 <pre>template<class T>
1317 bool operator==( scoped_array<T> const & p, std::nullptr_t ) noexcept;</pre>
1320 <div class="literalblock">
1321 <div class="content">
1322 <pre>template<class T>
1323 bool operator==( std::nullptr_t, scoped_array<T> const & p ) noexcept;</pre>
1326 <div class="paragraph">
1327 <p>Returns <code>p.get() == nullptr</code>.</p>
1329 <div class="literalblock">
1330 <div class="content">
1331 <pre>template<class T>
1332 bool operator!=( scoped_array<T> const & p, std::nullptr_t ) noexcept;</pre>
1335 <div class="literalblock">
1336 <div class="content">
1337 <pre>template<class T>
1338 bool operator!=( std::nullptr_t, scoped_array<T> const & p ) noexcept;</pre>
1341 <div class="paragraph">
1342 <p>Returns <code>p.get() != nullptr</code>.</p>
1349 <h2 id="shared_ptr">shared_ptr: Shared Ownership</h2>
1350 <div class="sectionbody">
1352 <h3 id="shared_ptr_description">Description</h3>
1353 <div class="paragraph">
1354 <p>The <code>shared_ptr</code> class template stores a pointer to a dynamically allocated object, typically with a C++ <code>new</code>-expression.
1355 The object pointed to is guaranteed to be deleted when the last <code>shared_ptr</code> pointing to it is destroyed or reset.</p>
1357 <div class="listingblock">
1358 <div class="title">Code Example 1. Using shared_ptr</div>
1359 <div class="content">
1360 <pre class="highlight"><code>shared_ptr<X> p1( new X );
1361 shared_ptr<void> p2( new int(5) );</code></pre>
1364 <div class="paragraph">
1365 <p><code>shared_ptr</code> deletes the exact pointer that has been passed at construction time, complete with its original type, regardless
1366 of the template parameter. In the second example above, when <code>p2</code> is destroyed or reset, it will call <code>delete</code> on the original
1367 <code>int*</code> that has been passed to the constructor, even though <code>p2</code> itself is of type <code>shared_ptr<void></code> and stores a pointer of
1368 type <code>void*</code>.</p>
1370 <div class="paragraph">
1371 <p>Every <code>shared_ptr</code> meets the <code>CopyConstructible</code>, <code>MoveConstructible</code>, <code>CopyAssignable</code> and <code>MoveAssignable</code> requirements of the
1372 C++ Standard Library, and can be used in standard library containers. Comparison operators are supplied so that <code>shared_ptr</code>
1373 works with the standard library’s associative containers.</p>
1375 <div class="paragraph">
1376 <p>Because the implementation uses reference counting, cycles of <code>shared_ptr</code> instances will not be reclaimed. For example, if <code>main()</code>
1377 holds a <code>shared_ptr</code> to <code>A</code>, which directly or indirectly holds a <code>shared_ptr</code> back to <code>A</code>, <code>A’s use count will be 2. Destruction
1378 of the original `shared_ptr</code> will leave <code>A</code> dangling with a use count of 1. Use <code><a href="#weak_ptr">weak_ptr</a></code> to "break cycles."</p>
1380 <div class="paragraph">
1381 <p>The class template is parameterized on <code>T</code>, the type of the object pointed to. <code>shared_ptr</code> and most of its member functions place
1382 no requirements on <code>T</code>; it is allowed to be an incomplete type, or <code>void</code>. Member functions that do place additional requirements
1383 (constructors, <code>reset</code>) are explicitly documented below.</p>
1385 <div class="paragraph">
1386 <p><code>shared_ptr<T></code> can be implicitly converted to <code>shared_ptr<U></code> whenever <code>T*</code> can be implicitly converted to <code>U*</code>. In particular,
1387 <code>shared_ptr<T></code> is implicitly convertible to <code>shared_ptr<T const></code>, to <code>shared_ptr<U></code> where <code>U</code> is an accessible base of <code>T</code>,
1388 and to <code>shared_ptr<void></code>.</p>
1390 <div class="paragraph">
1391 <p><code>shared_ptr</code> is now part of the C++11 Standard, as <code>std::shared_ptr</code>.</p>
1393 <div class="paragraph">
1394 <p>Starting with Boost release 1.53, <code>shared_ptr</code> can be used to hold a pointer to a dynamically allocated array. This is accomplished
1395 by using an array type (<code>T[]</code> or <code>T[N]</code>) as the template parameter. There is almost no difference between using an unsized array,
1396 <code>T[]</code>, and a sized array, <code>T[N]</code>; the latter just enables <code>operator[]</code> to perform a range check on the index.</p>
1398 <div class="listingblock">
1399 <div class="title">Code Example 2. Using shared_ptr with arrays</div>
1400 <div class="content">
1401 <pre class="highlight"><code>shared_ptr<double[1024]> p1( new double[1024] );
1402 shared_ptr<double[]> p2( new double[n] );</code></pre>
1407 <h3 id="shared_ptr_best_practices">Best Practices</h3>
1408 <div class="paragraph">
1409 <p>A simple guideline that nearly eliminates the possibility of memory leaks is: always use a named smart pointer variable to hold the result
1410 of <code>new</code>. Every occurence of the <code>new</code> keyword in the code should have the form:</p>
1412 <div class="literalblock">
1413 <div class="content">
1414 <pre>shared_ptr<T> p(new Y);</pre>
1417 <div class="paragraph">
1418 <p>It is, of course, acceptable to use another smart pointer in place of <code>shared_ptr</code> above; having <code>T</code> and <code>Y</code> be the same type, or passing
1419 arguments to the constructor of <code>Y</code> is also OK.</p>
1421 <div class="paragraph">
1422 <p>If you observe this guideline, it naturally follows that you will have no explicit <code>delete</code> statements; <code>try</code>/<code>catch</code> constructs will be rare.</p>
1424 <div class="paragraph">
1425 <p>Avoid using unnamed <code>shared_ptr</code> temporaries to save typing; to see why this is dangerous, consider this example:</p>
1427 <div class="listingblock">
1428 <div class="title">Code Example 3. Exception-safe and -unsafe use of shared_ptr</div>
1429 <div class="content">
1430 <pre class="highlight"><code>void f(shared_ptr<int>, int);
1435 shared_ptr<int> p( new int(2) );
1441 f( shared_ptr<int>( new int(2) ), g() );
1445 <div class="paragraph">
1446 <p>The function <code>ok</code> follows the guideline to the letter, whereas <code>bad</code> constructs the temporary <code>shared_ptr</code> in place, admitting the possibility of
1447 a memory leak. Since function arguments are evaluated in unspecified order, it is possible for <code>new int(2)</code> to be evaluated first, <code>g()</code> second,
1448 and we may never get to the <code>shared_ptr</code> constructor if <code>g</code> throws an exception. See <a href="http://www.gotw.ca/gotw/056.htm">Herb Sutter’s treatment</a> of
1449 the issue for more information.</p>
1451 <div class="paragraph">
1452 <p>The exception safety problem described above may also be eliminated by using the <code><a href="#make_shared">make_shared</a></code> or <code>allocate_shared</code> factory
1453 functions defined in <code><boost/smart_ptr/make_shared.hpp></code>. These factory functions also provide an efficiency benefit by consolidating allocations.</p>
1457 <h3 id="shared_ptr_synopsis">Synopsis</h3>
1458 <div class="paragraph">
1459 <p><code>shared_ptr</code> is defined in <code><boost/smart_ptr/shared_ptr.hpp></code>.</p>
1461 <div class="listingblock">
1462 <div class="content">
1463 <pre class="highlight"><code>namespace boost {
1465 class bad_weak_ptr: public std::exception;
1467 template<class T> class weak_ptr;
1469 template<class T> class shared_ptr {
1472 typedef /*see below*/ element_type;
1474 constexpr shared_ptr() noexcept;
1475 constexpr shared_ptr(std::nullptr_t) noexcept;
1477 template<class Y> explicit shared_ptr(Y * p);
1478 template<class Y, class D> shared_ptr(Y * p, D d);
1479 template<class Y, class D, class A> shared_ptr(Y * p, D d, A a);
1480 template<class D> shared_ptr(std::nullptr_t p, D d);
1481 template<class D, class A> shared_ptr(std::nullptr_t p, D d, A a);
1483 ~shared_ptr() noexcept;
1485 shared_ptr(shared_ptr const & r) noexcept;
1486 template<class Y> shared_ptr(shared_ptr<Y> const & r) noexcept;
1488 shared_ptr(shared_ptr && r) noexcept;
1489 template<class Y> shared_ptr(shared_ptr<Y> && r) noexcept;
1491 template<class Y> shared_ptr(shared_ptr<Y> const & r, element_type * p) noexcept;
1492 template<class Y> shared_ptr(shared_ptr<Y> && r, element_type * p) noexcept;
1494 template<class Y> explicit shared_ptr(weak_ptr<Y> const & r);
1496 template<class Y> explicit shared_ptr(std::auto_ptr<Y> & r);
1497 template<class Y> shared_ptr(std::auto_ptr<Y> && r);
1499 template<class Y, class D> shared_ptr(std::unique_ptr<Y, D> && r);
1501 shared_ptr & operator=(shared_ptr const & r) noexcept;
1502 template<class Y> shared_ptr & operator=(shared_ptr<Y> const & r) noexcept;
1504 shared_ptr & operator=(shared_ptr const && r) noexcept;
1505 template<class Y> shared_ptr & operator=(shared_ptr<Y> const && r) noexcept;
1507 template<class Y> shared_ptr & operator=(std::auto_ptr<Y> & r);
1508 template<class Y> shared_ptr & operator=(std::auto_ptr<Y> && r);
1510 template<class Y, class D> shared_ptr & operator=(std::unique_ptr<Y, D> && r);
1512 shared_ptr & operator=(std::nullptr_t) noexcept;
1514 void reset() noexcept;
1516 template<class Y> void reset(Y * p);
1517 template<class Y, class D> void reset(Y * p, D d);
1518 template<class Y, class D, class A> void reset(Y * p, D d, A a);
1520 template<class Y> void reset(shared_ptr<Y> const & r, element_type * p) noexcept;
1521 template<class Y> void reset(shared_ptr<Y> && r, element_type * p) noexcept;
1523 T & operator*() const noexcept; // only valid when T is not an array type
1524 T * operator->() const noexcept; // only valid when T is not an array type
1526 // only valid when T is an array type
1527 element_type & operator[](std::ptrdiff_t i) const noexcept;
1529 element_type * get() const noexcept;
1531 bool unique() const noexcept;
1532 long use_count() const noexcept;
1534 explicit operator bool() const noexcept;
1536 void swap(shared_ptr & b) noexcept;
1538 template<class Y> bool owner_before(shared_ptr<Y> const & rhs) const noexcept;
1539 template<class Y> bool owner_before(weak_ptr<Y> const & rhs) const noexcept;
1542 template<class T, class U>
1543 bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept;
1545 template<class T, class U>
1546 bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept;
1548 template<class T, class U>
1549 bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept;
1551 template<class T> bool operator==(shared_ptr<T> const & p, std::nullptr_t) noexcept;
1552 template<class T> bool operator==(std::nullptr_t, shared_ptr<T> const & p) noexcept;
1554 template<class T> bool operator!=(shared_ptr<T> const & p, std::nullptr_t) noexcept;
1555 template<class T> bool operator!=(std::nullptr_t, shared_ptr<T> const & p) noexcept;
1557 template<class T> void swap(shared_ptr<T> & a, shared_ptr<T> & b) noexcept;
1559 template<class T>
1560 typename shared_ptr<T>::element_type *
1561 get_pointer(shared_ptr<T> const & p) noexcept;
1563 template<class T, class U>
1564 shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r) noexcept;
1566 template<class T, class U>
1567 shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r) noexcept;
1569 template<class T, class U>
1570 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r) noexcept;
1572 template<class T, class U>
1573 shared_ptr<T> reinterpret_pointer_cast(shared_ptr<U> const & r) noexcept;
1575 template<class E, class T, class Y>
1576 std::basic_ostream<E, T> &
1577 operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p);
1579 template<class D, class T> D * get_deleter(shared_ptr<T> const & p) noexcept;
1581 template<class T> bool atomic_is_lock_free( shared_ptr<T> const * p ) noexcept;
1583 template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p ) noexcept;
1584 template<class T>
1585 shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, int ) noexcept;
1587 template<class T>
1588 void atomic_store( shared_ptr<T> * p, shared_ptr<T> r ) noexcept;
1589 template<class T>
1590 void atomic_store_explicit( shared_ptr<T> * p, shared_ptr<T> r, int ) noexcept;
1592 template<class T>
1593 shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r ) noexcept;
1594 template<class T>
1595 shared_ptr<T> atomic_exchange_explicit(
1596 shared_ptr<T> * p, shared_ptr<T> r, int ) noexcept;
1598 template<class T>
1599 bool atomic_compare_exchange(
1600 shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w ) noexcept;
1601 template<class T>
1602 bool atomic_compare_exchange_explicit(
1603 shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w, int, int ) noexcept;
1609 <h3 id="shared_ptr_members">Members</h3>
1611 <h4 id="shared_ptr_element_type">element_type</h4>
1612 <div class="listingblock">
1613 <div class="content">
1614 <pre class="highlight"><code>typedef ... element_type;</code></pre>
1617 <div class="paragraph">
1618 <p><code>element_type</code> is <code>T</code> when <code>T</code> is not an array type, and <code>U</code> when <code>T</code> is <code>U[]</code> or <code>U[N]</code>.</p>
1622 <h4 id="shared_ptr_default_constructor">default constructor</h4>
1623 <div class="listingblock">
1624 <div class="content">
1625 <pre class="highlight"><code>constexpr shared_ptr() noexcept;</code></pre>
1628 <div class="listingblock">
1629 <div class="content">
1630 <pre class="highlight"><code>constexpr shared_ptr(std::nullptr_t) noexcept;</code></pre>
1633 <div class="ulist none">
1639 <dt class="hdlist1">Effects</dt>
1641 <p>Constructs an empty <code>shared_ptr</code>.</p>
1643 <dt class="hdlist1">Postconditions</dt>
1645 <p><code>use_count() == 0 && get() == 0</code>.</p>
1654 <h4 id="shared_ptr_pointer_constructor">pointer constructor</h4>
1655 <div class="listingblock">
1656 <div class="content">
1657 <pre class="highlight"><code>template<class Y> explicit shared_ptr(Y * p);</code></pre>
1660 <div class="ulist none">
1666 <dt class="hdlist1">Requires</dt>
1668 <p><code>Y</code> must be a complete type. The expression <code>delete[] p</code>, when <code>T</code> is an array type, or <code>delete p</code>, when <code>T</code> is not an array type,
1669 must be well-formed, well-defined, and not throw exceptions. When <code>T</code> is <code>U[N]</code>, <code>Y(*)[N]</code> must be convertible to <code>T*</code>; when <code>T</code> is <code>U[]</code>, <code>Y(*)[]</code>
1670 must be convertible to <code>T*</code>; otherwise, <code>Y*</code> must be convertible to <code>T*</code>.</p>
1672 <dt class="hdlist1">Effects</dt>
1674 <p>When <code>T</code> is not an array type, constructs a <code>shared_ptr</code> that owns the pointer <code>p</code>. Otherwise, constructs a <code>shared_ptr</code> that owns <code>p</code> and
1675 a deleter of an unspecified type that calls <code>delete[] p</code>.</p>
1677 <dt class="hdlist1">Postconditions</dt>
1679 <p><code>use_count() == 1 && get() == p</code>. If <code>T</code> is not an array type and <code>p</code> is unambiguously convertible to <code>enable_shared_from_this<V>*</code>
1680 for some <code>V</code>, <code>p->shared_from_this()</code> returns a copy of <code>*this</code>.</p>
1682 <dt class="hdlist1">Throws</dt>
1684 <p><code>std::bad_alloc</code>, or an implementation-defined exception when a resource other than memory could not be obtained.</p>
1686 <dt class="hdlist1">Exception safety</dt>
1688 <p>If an exception is thrown, the constructor calls <code>delete[] p</code>, when <code>T</code> is an array type, or <code>delete p</code>, when <code>T</code> is not an array type.</p>
1695 <div class="admonitionblock note">
1699 <div class="title">Note</div>
1701 <td class="content">
1702 <code>p</code> must be a pointer to an object that was allocated via a C++ <code>new</code> expression or be 0. The postcondition that use count is 1 holds even if <code>p</code>
1703 is 0; invoking <code>delete</code> on a pointer that has a value of 0 is harmless.
1708 <div class="admonitionblock note">
1712 <div class="title">Note</div>
1714 <td class="content">
1715 This constructor is a template in order to remember the actual pointer type passed. The destructor will call delete with the same pointer, complete
1716 with its original type, even when <code>T</code> does not have a virtual destructor, or is <code>void</code>.
1723 <h4 id="shared_ptr_constructors_taking_a_deleter">constructors taking a deleter</h4>
1724 <div class="listingblock">
1725 <div class="content">
1726 <pre class="highlight"><code>template<class Y, class D> shared_ptr(Y * p, D d);</code></pre>
1729 <div class="listingblock">
1730 <div class="content">
1731 <pre class="highlight"><code>template<class Y, class D, class A> shared_ptr(Y * p, D d, A a);</code></pre>
1734 <div class="listingblock">
1735 <div class="content">
1736 <pre class="highlight"><code>template<class D> shared_ptr(std::nullptr_t p, D d);</code></pre>
1739 <div class="listingblock">
1740 <div class="content">
1741 <pre class="highlight"><code>template<class D, class A> shared_ptr(std::nullptr_t p, D d, A a);</code></pre>
1744 <div class="ulist none">
1750 <dt class="hdlist1">Requires</dt>
1752 <p><code>D</code> must be <code>CopyConstructible</code>. The copy constructor and destructor of <code>D</code> must not throw. The expression <code>d(p)</code> must be well-formed, well-defined,
1753 and not throw exceptions. <code>A</code> must be an <code>Allocator</code>, as described in section Allocator Requirements [allocator.requirements] of the C++ Standard.
1754 When <code>T</code> is <code>U[N]</code>, <code>Y(*)[N]</code> must be convertible to <code>T*</code>; when <code>T</code> is <code>U[]</code>, <code>Y(*)[]</code> must be convertible to <code>T*</code>; otherwise, <code>Y*</code> must be convertible to <code>T*</code>.</p>
1756 <dt class="hdlist1">Effects</dt>
1758 <p>Constructs a <code>shared_ptr</code> that owns the pointer <code>p</code> and the deleter <code>d</code>. The constructors taking an allocator a allocate memory using a copy of <code>a</code>.</p>
1760 <dt class="hdlist1">Postconditions</dt>
1762 <p><code>use_count() == 1 && get() == p</code>. If <code>T</code> is not an array type and <code>p</code> is unambiguously convertible to <code>enable_shared_from_this<V>*</code> for some <code>V</code>,
1763 <code>p->shared_from_this()</code> returns a copy of <code>*this</code>.</p>
1765 <dt class="hdlist1">Throws</dt>
1767 <p><code>std::bad_alloc</code>, or an implementation-defined exception when a resource other than memory could not be obtained.</p>
1769 <dt class="hdlist1">Exception safety</dt>
1771 <p>If an exception is thrown, <code>d(p)</code> is called.</p>
1778 <div class="admonitionblock note">
1782 <div class="title">Note</div>
1784 <td class="content">
1785 When the the time comes to delete the object pointed to by <code>p</code>, the stored copy of <code>d</code> is invoked with the stored copy of <code>p</code> as an argument.
1790 <div class="admonitionblock note">
1794 <div class="title">Note</div>
1796 <td class="content">
1797 Custom deallocators allow a factory function returning a <code>shared_ptr</code> to insulate the user from its memory allocation strategy. Since the deallocator
1798 is not part of the type, changing the allocation strategy does not break source or binary compatibility, and does not require a client recompilation. For example,
1799 a "no-op" deallocator is useful when returning a <code>shared_ptr</code> to a statically allocated object, and other variations allow a <code>shared_ptr</code> to be used as a wrapper
1800 for another smart pointer, easing interoperability.
1805 <div class="admonitionblock note">
1809 <div class="title">Note</div>
1811 <td class="content">
1812 The requirement that the copy constructor of <code>D</code> does not throw comes from the pass by value. If the copy constructor throws, the pointer would leak.
1819 <h4 id="shared_ptr_copy_and_converting_constructors">copy and converting constructors</h4>
1820 <div class="listingblock">
1821 <div class="content">
1822 <pre class="highlight"><code>shared_ptr(shared_ptr const & r) noexcept;</code></pre>
1825 <div class="listingblock">
1826 <div class="content">
1827 <pre class="highlight"><code>template<class Y> shared_ptr(shared_ptr<Y> const & r) noexcept;</code></pre>
1830 <div class="ulist none">
1836 <dt class="hdlist1">Requires</dt>
1838 <p><code>Y*</code> should be convertible to <code>T*</code>.</p>
1840 <dt class="hdlist1">Effects</dt>
1842 <p>If <code>r</code> is empty, constructs an empty <code>shared_ptr</code>; otherwise, constructs a <code>shared_ptr</code> that shares ownership with <code>r</code>.</p>
1844 <dt class="hdlist1">Postconditions</dt>
1846 <p><code>get() == r.get() && use_count() == r.use_count()</code>.</p>
1855 <h4 id="shared_ptr_move_constructors">move constructors</h4>
1856 <div class="listingblock">
1857 <div class="content">
1858 <pre class="highlight"><code>shared_ptr(shared_ptr && r) noexcept;</code></pre>
1861 <div class="listingblock">
1862 <div class="content">
1863 <pre class="highlight"><code>template<class Y> shared_ptr(shared_ptr<Y> && r) noexcept;</code></pre>
1866 <div class="ulist none">
1872 <dt class="hdlist1">Requires</dt>
1874 <p><code>Y*</code> should be convertible to <code>T*</code>.</p>
1876 <dt class="hdlist1">Effects</dt>
1878 <p>Move-constructs a <code>shared_ptr</code> from <code>r</code>.</p>
1880 <dt class="hdlist1">Postconditions</dt>
1882 <p><code>*this</code> contains the old value of <code>r</code>. <code>r</code> is empty and <code>r.get() == 0</code>.</p>
1891 <h4 id="shared_ptr_aliasing_constructor">aliasing constructor</h4>
1892 <div class="listingblock">
1893 <div class="content">
1894 <pre class="highlight"><code>template<class Y> shared_ptr(shared_ptr<Y> const & r, element_type * p) noexcept;</code></pre>
1897 <div class="ulist none">
1903 <dt class="hdlist1">Effects</dt>
1905 <p>Copy-constructs a <code>shared_ptr</code> from <code>r</code>, while storing <code>p</code> instead.</p>
1907 <dt class="hdlist1">Postconditions</dt>
1909 <p><code>get() == p && use_count() == r.use_count()</code>.</p>
1918 <h4 id="shared_ptr_aliasing_move_constructor">aliasing move constructor</h4>
1919 <div class="listingblock">
1920 <div class="content">
1921 <pre class="highlight"><code>template<class Y> shared_ptr(shared_ptr<Y> && r, element_type * p) noexcept;</code></pre>
1924 <div class="ulist none">
1930 <dt class="hdlist1">Effects</dt>
1932 <p>Move-constructs a <code>shared_ptr</code> from <code>r</code>, while storing <code>p</code> instead.</p>
1934 <dt class="hdlist1">Postconditions</dt>
1936 <p><code>get() == p</code> and <code>use_count()</code> equals the old count of <code>r</code>. <code>r</code> is empty and <code>r.get() == 0</code>.</p>
1945 <h4 id="shared_ptr_weak_ptr_constructor">weak_ptr constructor</h4>
1946 <div class="listingblock">
1947 <div class="content">
1948 <pre class="highlight"><code>template<class Y> explicit shared_ptr(weak_ptr<Y> const & r);</code></pre>
1951 <div class="ulist none">
1957 <dt class="hdlist1">Requires</dt>
1959 <p><code>Y*</code> should be convertible to <code>T*</code>.</p>
1961 <dt class="hdlist1">Effects</dt>
1963 <p>Constructs a <code>shared_ptr</code> that shares ownership with <code>r</code> and stores a copy of the pointer stored in <code>r</code>.</p>
1965 <dt class="hdlist1">Postconditions</dt>
1967 <p><code>use_count() == r.use_count()</code>.</p>
1969 <dt class="hdlist1">Throws</dt>
1971 <p><code>bad_weak_ptr</code> when <code>r.use_count() == 0</code>.</p>
1973 <dt class="hdlist1">Exception safety</dt>
1975 <p>If an exception is thrown, the constructor has no effect.</p>
1984 <h4 id="shared_ptr_auto_ptr_constructors">auto_ptr constructors</h4>
1985 <div class="listingblock">
1986 <div class="content">
1987 <pre class="highlight"><code>template<class Y> shared_ptr(std::auto_ptr<Y> & r);</code></pre>
1990 <div class="listingblock">
1991 <div class="content">
1992 <pre class="highlight"><code>template<class Y> shared_ptr(std::auto_ptr<Y> && r);</code></pre>
1995 <div class="ulist none">
2001 <dt class="hdlist1">Requires</dt>
2003 <p><code>Y*</code> should be convertible to <code>T*</code>.</p>
2005 <dt class="hdlist1">Effects</dt>
2007 <p>Constructs a <code>shared_ptr</code>, as if by storing a copy of <code>r.release()</code>.</p>
2009 <dt class="hdlist1">Postconditions</dt>
2011 <p><code>use_count() == 1</code>.</p>
2013 <dt class="hdlist1">Throws</dt>
2015 <p><code>std::bad_alloc</code>, or an implementation-defined exception when a resource other than memory could not be obtained.</p>
2017 <dt class="hdlist1">Exception safety</dt>
2019 <p>If an exception is thrown, the constructor has no effect.</p>
2028 <h4 id="shared_ptr_unique_ptr_constructor">unique_ptr constructor</h4>
2029 <div class="listingblock">
2030 <div class="content">
2031 <pre class="highlight"><code>template<class Y, class D> shared_ptr(std::unique_ptr<Y, D> && r);</code></pre>
2034 <div class="ulist none">
2040 <dt class="hdlist1">Requires</dt>
2042 <p><code>Y*</code> should be convertible to <code>T*</code>.</p>
2044 <dt class="hdlist1">Effects</dt>
2049 <p>When <code>r.get() == 0</code>, equivalent to <code>shared_ptr()</code>;</p>
2052 <p>When <code>D</code> is not a reference type, equivalent to <code>shared_ptr(r.release(), r.get_deleter())</code>;</p>
2055 <p>Otherwise, equivalent to <code>shared_ptr(r.release(), del)</code>, where <code>del</code> is a deleter that stores the reference <code>rd</code> returned
2056 from <code>r.get_deleter()</code> and <code>del(p)</code> calls <code>rd(p)</code>.</p>
2061 <dt class="hdlist1">Throws</dt>
2063 <p><code>std::bad_alloc</code>, or an implementation-defined exception when a resource other than memory could not be obtained.</p>
2065 <dt class="hdlist1">Exception safety</dt>
2067 <p>If an exception is thrown, the constructor has no effect.</p>
2076 <h4 id="shared_ptr_destructor">destructor</h4>
2077 <div class="listingblock">
2078 <div class="content">
2079 <pre class="highlight"><code>~shared_ptr() noexcept;</code></pre>
2082 <div class="ulist none">
2088 <dt class="hdlist1">Effects</dt>
2093 <p>If <code>*this</code> is empty, or shares ownership with another <code>shared_ptr</code> instance (<code>use_count() > 1</code>), there are no side effects.</p>
2096 <p>Otherwise, if <code>*this</code> owns a pointer <code>p</code> and a deleter <code>d</code>, <code>d(p)</code> is called.</p>
2099 <p>Otherwise, <code>*this</code> owns a pointer <code>p</code>, and <code>delete p</code> is called.</p>
2111 <h4 id="shared_ptr_assignment">assignment</h4>
2112 <div class="listingblock">
2113 <div class="content">
2114 <pre class="highlight"><code>shared_ptr & operator=(shared_ptr const & r) noexcept;</code></pre>
2117 <div class="listingblock">
2118 <div class="content">
2119 <pre class="highlight"><code>template<class Y> shared_ptr & operator=(shared_ptr<Y> const & r) noexcept;</code></pre>
2122 <div class="listingblock">
2123 <div class="content">
2124 <pre class="highlight"><code>template<class Y> shared_ptr & operator=(std::auto_ptr<Y> & r);</code></pre>
2127 <div class="ulist none">
2133 <dt class="hdlist1">Effects</dt>
2135 <p>Equivalent to <code>shared_ptr(r).swap(*this)</code>.</p>
2137 <dt class="hdlist1">Returns</dt>
2139 <p><code>*this</code>.</p>
2146 <div class="admonitionblock note">
2150 <div class="title">Note</div>
2152 <td class="content">
2153 The use count updates caused by the temporary object construction and destruction are not considered observable side effects,
2154 and the implementation is free to meet the effects (and the implied guarantees) via different means, without creating a temporary.
2159 <div class="admonitionblock note">
2163 <div class="title">Note</div>
2165 <td class="content">
2166 <div class="paragraph">
2167 <p>In particular, in the example:</p>
2169 <div class="listingblock">
2170 <div class="content">
2171 <pre class="highlight"><code>shared_ptr<int> p(new int);
2172 shared_ptr<void> q(p);
2177 <div class="paragraph">
2178 <p>both assignments may be no-ops.</p>
2184 <div class="listingblock">
2185 <div class="content">
2186 <pre class="highlight"><code>shared_ptr & operator=(shared_ptr && r) noexcept;</code></pre>
2189 <div class="listingblock">
2190 <div class="content">
2191 <pre class="highlight"><code>template<class Y> shared_ptr & operator=(shared_ptr<Y> && r) noexcept;</code></pre>
2194 <div class="listingblock">
2195 <div class="content">
2196 <pre class="highlight"><code>template<class Y> shared_ptr & operator=(std::auto_ptr<Y> && r);</code></pre>
2199 <div class="listingblock">
2200 <div class="content">
2201 <pre class="highlight"><code>template<class Y, class D> shared_ptr & operator=(std::unique_ptr<Y, D> && r);</code></pre>
2204 <div class="ulist none">
2210 <dt class="hdlist1">Effects</dt>
2212 <p>Equivalent to <code>shared_ptr(std::move(r)).swap(*this)</code>.</p>
2214 <dt class="hdlist1">Returns</dt>
2216 <p><code>*this</code>.</p>
2223 <div class="listingblock">
2224 <div class="content">
2225 <pre class="highlight"><code>shared_ptr & operator=(std::nullptr_t) noexcept;</code></pre>
2228 <div class="ulist none">
2234 <dt class="hdlist1">Effects</dt>
2236 <p>Equivalent to <code>shared_ptr().swap(*this)</code>.</p>
2238 <dt class="hdlist1">Returns</dt>
2240 <p><code>*this</code>.</p>
2249 <h4 id="shared_ptr_reset">reset</h4>
2250 <div class="listingblock">
2251 <div class="content">
2252 <pre class="highlight"><code>void reset() noexcept;</code></pre>
2255 <div class="ulist none">
2261 <dt class="hdlist1">Effects</dt>
2263 <p>Equivalent to <code>shared_ptr().swap(*this)</code>.</p>
2270 <div class="listingblock">
2271 <div class="content">
2272 <pre class="highlight"><code>template<class Y> void reset(Y * p);</code></pre>
2275 <div class="ulist none">
2281 <dt class="hdlist1">Effects</dt>
2283 <p>Equivalent to <code>shared_ptr(p).swap(*this)</code>.</p>
2290 <div class="listingblock">
2291 <div class="content">
2292 <pre class="highlight"><code>template<class Y, class D> void reset(Y * p, D d);</code></pre>
2295 <div class="ulist none">
2301 <dt class="hdlist1">Effects</dt>
2303 <p>Equivalent to <code>shared_ptr(p, d).swap(*this)</code>.</p>
2310 <div class="listingblock">
2311 <div class="content">
2312 <pre class="highlight"><code>template<class Y, class D, class A> void reset(Y * p, D d, A a);</code></pre>
2315 <div class="ulist none">
2321 <dt class="hdlist1">Effects</dt>
2323 <p>Equivalent to <code>shared_ptr(p, d, a).swap(*this)</code>.</p>
2330 <div class="listingblock">
2331 <div class="content">
2332 <pre class="highlight"><code>template<class Y> void reset(shared_ptr<Y> const & r, element_type * p) noexcept;</code></pre>
2335 <div class="ulist none">
2341 <dt class="hdlist1">Effects</dt>
2343 <p>Equivalent to <code>shared_ptr(r, p).swap(*this)</code>.</p>
2350 <div class="listingblock">
2351 <div class="content">
2352 <pre class="highlight"><code>template<class Y> void reset(shared_ptr<Y> && r, element_type * p) noexcept;</code></pre>
2355 <div class="ulist none">
2361 <dt class="hdlist1">Effects</dt>
2363 <p>Equivalent to <code>shared_ptr(std::move(r), p).swap(*this)</code>.</p>
2372 <h4 id="shared_ptr_indirection">indirection</h4>
2373 <div class="listingblock">
2374 <div class="content">
2375 <pre class="highlight"><code>T & operator*() const noexcept;</code></pre>
2378 <div class="ulist none">
2384 <dt class="hdlist1">Requires</dt>
2386 <p><code>T</code> should not be an array type. The stored pointer must not be 0.</p>
2388 <dt class="hdlist1">Returns</dt>
2390 <p><code>*get()</code>.</p>
2397 <div class="listingblock">
2398 <div class="content">
2399 <pre class="highlight"><code>T * operator->() const noexcept;</code></pre>
2402 <div class="ulist none">
2408 <dt class="hdlist1">Requires</dt>
2410 <p><code>T</code> should not be an array type. The stored pointer must not be 0.</p>
2412 <dt class="hdlist1">Returns</dt>
2414 <p><code>get()</code>.</p>
2421 <div class="listingblock">
2422 <div class="content">
2423 <pre class="highlight"><code>element_type & operator[](std::ptrdiff_t i) const noexcept;</code></pre>
2426 <div class="ulist none">
2432 <dt class="hdlist1">Requires</dt>
2434 <p><code>T</code> should be an array type. The stored pointer must not be 0. <code>i >= 0</code>. If <code>T</code> is <code>U[N]</code>, <code>i < N</code>.</p>
2436 <dt class="hdlist1">Returns</dt>
2438 <p><code>get()[i]</code>.</p>
2447 <h4 id="shared_ptr_get">get</h4>
2448 <div class="listingblock">
2449 <div class="content">
2450 <pre class="highlight"><code>element_type * get() const noexcept;</code></pre>
2453 <div class="ulist none">
2459 <dt class="hdlist1">Returns</dt>
2461 <p>The stored pointer.</p>
2470 <h4 id="shared_ptr_unique">unique</h4>
2471 <div class="listingblock">
2472 <div class="content">
2473 <pre class="highlight"><code>bool unique() const noexcept;</code></pre>
2476 <div class="ulist none">
2482 <dt class="hdlist1">Returns</dt>
2484 <p><code>use_count() == 1</code>.</p>
2493 <h4 id="shared_ptr_use_count">use_count</h4>
2494 <div class="listingblock">
2495 <div class="content">
2496 <pre class="highlight"><code>long use_count() const noexcept;</code></pre>
2499 <div class="ulist none">
2505 <dt class="hdlist1">Returns</dt>
2507 <p>The number of <code>shared_ptr</code> objects, <code>*this</code> included, that share ownership with <code>*this</code>, or 0 when <code>*this</code> is empty.</p>
2516 <h4 id="shared_ptr_conversions">conversions</h4>
2517 <div class="listingblock">
2518 <div class="content">
2519 <pre class="highlight"><code>explicit operator bool() const noexcept;</code></pre>
2522 <div class="ulist none">
2528 <dt class="hdlist1">Returns</dt>
2530 <p><code>get() != 0</code>.</p>
2537 <div class="admonitionblock note">
2541 <div class="title">Note</div>
2543 <td class="content">
2544 This conversion operator allows <code>shared_ptr</code> objects to be used in boolean contexts, like <code>if(p && p->valid()) {}</code>.
2549 <div class="admonitionblock note">
2553 <div class="title">Note</div>
2555 <td class="content">
2556 The conversion to <code>bool</code> is not merely syntactic sugar. It allows <code>shared_ptr</code> variables to be declared in conditions when using
2557 <code>dynamic_pointer_cast</code> or <code>weak_ptr::lock</code>.
2562 <div class="admonitionblock note">
2566 <div class="title">Note</div>
2568 <td class="content">
2569 On C++03 compilers, the return value is of an unspecified type.
2576 <h4 id="shared_ptr_swap">swap</h4>
2577 <div class="listingblock">
2578 <div class="content">
2579 <pre class="highlight"><code>void swap(shared_ptr & b) noexcept;</code></pre>
2582 <div class="ulist none">
2588 <dt class="hdlist1">Effects</dt>
2590 <p>Exchanges the contents of the two smart pointers.</p>
2599 <h4 id="shared_ptr_owner_before">owner_before</h4>
2600 <div class="listingblock">
2601 <div class="content">
2602 <pre class="highlight"><code>template<class Y> bool owner_before(shared_ptr<Y> const & rhs) const noexcept;</code></pre>
2605 <div class="listingblock">
2606 <div class="content">
2607 <pre class="highlight"><code>template<class Y> bool owner_before(weak_ptr<Y> const & rhs) const noexcept;</code></pre>
2610 <div class="ulist none">
2616 <dt class="hdlist1">Effects</dt>
2618 <p>See the description of <code>operator<</code>.</p>
2628 <h3 id="shared_ptr_free_functions">Free Functions</h3>
2630 <h4 id="shared_ptr_comparison">comparison</h4>
2631 <div class="listingblock">
2632 <div class="content">
2633 <pre class="highlight"><code>template<class T, class U>
2634 bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept;</code></pre>
2637 <div class="ulist none">
2643 <dt class="hdlist1">Returns</dt>
2645 <p><code>a.get() == b.get()</code>.</p>
2652 <div class="listingblock">
2653 <div class="content">
2654 <pre class="highlight"><code>template<class T, class U>
2655 bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept;</code></pre>
2658 <div class="ulist none">
2664 <dt class="hdlist1">Returns</dt>
2666 <p><code>a.get() != b.get()</code>.</p>
2673 <div class="listingblock">
2674 <div class="content">
2675 <pre class="highlight"><code>template<class T> bool operator==(shared_ptr<T> const & p, std::nullptr_t) noexcept;</code></pre>
2678 <div class="listingblock">
2679 <div class="content">
2680 <pre class="highlight"><code>template<class T> bool operator==(std::nullptr_t, shared_ptr<T> const & p) noexcept;</code></pre>
2683 <div class="ulist none">
2689 <dt class="hdlist1">Returns</dt>
2691 <p><code>p.get() == 0</code>.</p>
2698 <div class="listingblock">
2699 <div class="content">
2700 <pre class="highlight"><code>template<class T> bool operator!=(shared_ptr<T> const & p, std::nullptr_t) noexcept;</code></pre>
2703 <div class="listingblock">
2704 <div class="content">
2705 <pre class="highlight"><code>template<class T> bool operator!=(std::nullptr_t, shared_ptr<T> const & p) noexcept;</code></pre>
2708 <div class="ulist none">
2714 <dt class="hdlist1">Returns</dt>
2716 <p><code>p.get() != 0</code>.</p>
2723 <div class="listingblock">
2724 <div class="content">
2725 <pre class="highlight"><code>template<class T, class U>
2726 bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept;</code></pre>
2729 <div class="ulist none">
2735 <dt class="hdlist1">Returns</dt>
2737 <p>An unspecified value such that</p>
2741 <p><code>operator<</code> is a strict weak ordering as described in section [lib.alg.sorting] of the C++ standard;</p>
2744 <p>under the equivalence relation defined by <code>operator<</code>, <code>!(a < b) && !(b < a)</code>, two <code>shared_ptr</code> instances
2745 are equivalent if and only if they share ownership or are both empty.</p>
2755 <div class="admonitionblock note">
2759 <div class="title">Note</div>
2761 <td class="content">
2762 Allows <code>shared_ptr</code> objects to be used as keys in associative containers.
2767 <div class="admonitionblock note">
2771 <div class="title">Note</div>
2773 <td class="content">
2774 The rest of the comparison operators are omitted by design.
2781 <h4 id="shared_ptr_swap_2">swap</h4>
2782 <div class="listingblock">
2783 <div class="content">
2784 <pre class="highlight"><code>template<class T> void swap(shared_ptr<T> & a, shared_ptr<T> & b) noexcept;</code></pre>
2787 <div class="ulist none">
2793 <dt class="hdlist1">Effects</dt>
2795 <p>Equivalent to <code>a.swap(b)</code>.</p>
2804 <h4 id="shared_ptr_get_pointer">get_pointer</h4>
2805 <div class="listingblock">
2806 <div class="content">
2807 <pre class="highlight"><code>template<class T>
2808 typename shared_ptr<T>::element_type *
2809 get_pointer(shared_ptr<T> const & p) noexcept;</code></pre>
2812 <div class="ulist none">
2818 <dt class="hdlist1">Returns</dt>
2820 <p><code>p.get()</code>.</p>
2827 <div class="admonitionblock note">
2831 <div class="title">Note</div>
2833 <td class="content">
2834 Provided as an aid to generic programming. Used by <code>mem_fn</code>.
2841 <h4 id="shared_ptr_static_pointer_cast">static_pointer_cast</h4>
2842 <div class="listingblock">
2843 <div class="content">
2844 <pre class="highlight"><code>template<class T, class U>
2845 shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r) noexcept;</code></pre>
2848 <div class="ulist none">
2854 <dt class="hdlist1">Requires</dt>
2856 <p>The expression <code>static_cast<T*>( (U*)0 )</code> must be well-formed.</p>
2858 <dt class="hdlist1">Returns</dt>
2860 <p><code>shared_ptr<T>( r, static_cast<typename shared_ptr<T>::element_type*>(r.get()) )</code>.</p>
2867 <div class="admonitionblock caution">
2871 <div class="title">Caution</div>
2873 <td class="content">
2874 The seemingly equivalent expression <code>shared_ptr<T>(static_cast<T*>(r.get()))</code> will eventually
2875 result in undefined behavior, attempting to delete the same object twice.
2882 <h4 id="shared_ptr_const_pointer_cast">const_pointer_cast</h4>
2883 <div class="listingblock">
2884 <div class="content">
2885 <pre class="highlight"><code>template<class T, class U>
2886 shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r) noexcept;</code></pre>
2889 <div class="ulist none">
2895 <dt class="hdlist1">Requires</dt>
2897 <p>The expression <code>const_cast<T*>( (U*)0 )</code> must be well-formed.</p>
2899 <dt class="hdlist1">Returns</dt>
2901 <p><code>shared_ptr<T>( r, const_cast<typename shared_ptr<T>::element_type*>(r.get()) )</code>.</p>
2910 <h4 id="shared_ptr_dynamic_pointer_cast">dynamic_pointer_cast</h4>
2911 <div class="listingblock">
2912 <div class="content">
2913 <pre class="highlight"><code>template<class T, class U>
2914 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r) noexcept;</code></pre>
2917 <div class="ulist none">
2923 <dt class="hdlist1">Requires</dt>
2925 <p>The expression <code>dynamic_cast<T*>( (U*)0 )</code> must be well-formed.</p>
2927 <dt class="hdlist1">Returns</dt>
2932 <p>When <code>dynamic_cast<typename shared_ptr<T>::element_type*>(r.get())</code> returns a nonzero value <code>p</code>, <code>shared_ptr<T>(r, p)</code>;</p>
2935 <p>Otherwise, <code>shared_ptr<T>()</code>.</p>
2947 <h4 id="shared_ptr_reinterpret_pointer_cast">reinterpret_pointer_cast</h4>
2948 <div class="listingblock">
2949 <div class="content">
2950 <pre class="highlight"><code>template<class T, class U>
2951 shared_ptr<T> reinterpret_pointer_cast(shared_ptr<U> const & r) noexcept;</code></pre>
2954 <div class="ulist none">
2960 <dt class="hdlist1">Requires</dt>
2962 <p>The expression <code>reinterpret_cast<T*>( (U*)0 )</code> must be well-formed.</p>
2964 <dt class="hdlist1">Returns</dt>
2966 <p><code>shared_ptr<T>( r, reinterpret_cast<typename shared_ptr<T>::element_type*>(r.get()) )</code>.</p>
2975 <h4 id="shared_ptr_operator">operator<<</h4>
2976 <div class="listingblock">
2977 <div class="content">
2978 <pre class="highlight"><code>template<class E, class T, class Y>
2979 std::basic_ostream<E, T> &
2980 operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p);</code></pre>
2983 <div class="ulist none">
2989 <dt class="hdlist1">Effects</dt>
2991 <p><code>os << p.get();</code>.</p>
2993 <dt class="hdlist1">Returns</dt>
2995 <p><code>os</code>.</p>
3004 <h4 id="shared_ptr_get_deleter">get_deleter</h4>
3005 <div class="listingblock">
3006 <div class="content">
3007 <pre class="highlight"><code>template<class D, class T>
3008 D * get_deleter(shared_ptr<T> const & p) noexcept;</code></pre>
3011 <div class="ulist none">
3017 <dt class="hdlist1">Returns</dt>
3019 <p>If <code>*this</code> owns a deleter <code>d</code> of type (cv-unqualified) <code>D</code>, returns <code>&d</code>; otherwise returns 0.</p>
3028 <h4 id="shared_ptr_atomic_access">Atomic Access</h4>
3029 <div class="admonitionblock note">
3033 <div class="title">Note</div>
3035 <td class="content">
3036 The function in this section are atomic with respect to the first <code>shared_ptr</code> argument,
3037 identified by <code>*p</code>. Concurrent access to the same <code>shared_ptr</code> instance is not a data race, if
3038 done exclusively by the functions in this section.
3043 <div class="listingblock">
3044 <div class="content">
3045 <pre class="highlight"><code>template<class T> bool atomic_is_lock_free( shared_ptr<T> const * p ) noexcept;</code></pre>
3048 <div class="ulist none">
3054 <dt class="hdlist1">Returns</dt>
3056 <p><code>false</code>.</p>
3063 <div class="admonitionblock note">
3067 <div class="title">Note</div>
3069 <td class="content">
3070 This implementation is not lock-free.
3075 <div class="listingblock">
3076 <div class="content">
3077 <pre class="highlight"><code>template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p ) noexcept;</code></pre>
3080 <div class="listingblock">
3081 <div class="content">
3082 <pre class="highlight"><code>template<class T> shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, int ) noexcept;</code></pre>
3085 <div class="ulist none">
3091 <dt class="hdlist1">Returns</dt>
3093 <p><code>*p</code>.</p>
3100 <div class="admonitionblock note">
3104 <div class="title">Note</div>
3106 <td class="content">
3107 The <code>int</code> argument is the <code>memory_order</code>, but this implementation does not use it, as it’s lock-based
3108 and therefore always sequentially consistent.
3113 <div class="listingblock">
3114 <div class="content">
3115 <pre class="highlight"><code>template<class T>
3116 void atomic_store( shared_ptr<T> * p, shared_ptr<T> r ) noexcept;</code></pre>
3119 <div class="listingblock">
3120 <div class="content">
3121 <pre class="highlight"><code>template<class T>
3122 void atomic_store_explicit( shared_ptr<T> * p, shared_ptr<T> r, int ) noexcept;</code></pre>
3125 <div class="ulist none">
3131 <dt class="hdlist1">Effects</dt>
3133 <p><code>p->swap(r)</code>.</p>
3140 <div class="listingblock">
3141 <div class="content">
3142 <pre class="highlight"><code>template<class T>
3143 shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r ) noexcept;</code></pre>
3146 <div class="listingblock">
3147 <div class="content">
3148 <pre class="highlight"><code>template<class T>
3149 shared_ptr<T> atomic_exchange_explicit(
3150 shared_ptr<T> * p, shared_ptr<T> r, int ) noexcept;</code></pre>
3153 <div class="ulist none">
3159 <dt class="hdlist1">Effects</dt>
3161 <p><code>p->swap(r)</code>.</p>
3163 <dt class="hdlist1">Returns</dt>
3165 <p>The old value of <code>*p</code>.</p>
3172 <div class="listingblock">
3173 <div class="content">
3174 <pre class="highlight"><code>template<class T>
3175 bool atomic_compare_exchange(
3176 shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w ) noexcept;</code></pre>
3179 <div class="listingblock">
3180 <div class="content">
3181 <pre class="highlight"><code>template<class T>
3182 bool atomic_compare_exchange_explicit(
3183 shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w, int, int ) noexcept;</code></pre>
3186 <div class="ulist none">
3192 <dt class="hdlist1">Effects</dt>
3194 <p>If <code>*p</code> is equivalent to <code>*v</code>, assigns <code>w</code> to <code>*p</code>, otherwise assigns <code>*p</code> to <code>*v</code>.</p>
3196 <dt class="hdlist1">Returns</dt>
3198 <p><code>true</code> if <code>*p</code> was equivalent to <code>*v</code>, <code>false</code> otherwise.</p>
3200 <dt class="hdlist1">Remarks</dt>
3202 <p>Two <code>shared_ptr</code> instances are equivalent if they store the same pointer value and <em>share ownership</em>.</p>
3212 <h3 id="shared_ptr_example">Example</h3>
3213 <div class="paragraph">
3214 <p>See <a href="../../example/shared_ptr_example.cpp">shared_ptr_example.cpp</a> for a complete example program. The program builds a
3215 <code>std::vector</code> and <code>std::set</code> of <code>shared_ptr</code> objects.</p>
3217 <div class="paragraph">
3218 <p>Note that after the containers have been populated, some of the <code>shared_ptr</code> objects will have a use count of 1 rather than
3219 a use count of 2, since the set is a <code>std::set</code> rather than a <code>std::multiset</code>, and thus does not contain duplicate entries.
3220 Furthermore, the use count may be even higher at various times while <code>push_back</code> and <code>insert</code> container operations are performed.
3221 More complicated yet, the container operations may throw exceptions under a variety of circumstances. Getting the memory management
3222 and exception handling in this example right without a smart pointer would be a nightmare.</p>
3226 <h3 id="shared_ptr_handlebody_idiom">Handle/Body Idiom</h3>
3227 <div class="paragraph">
3228 <p>One common usage of <code>shared_ptr</code> is to implement a handle/body (also called pimpl) idiom which avoids exposing the body (implementation)
3229 in the header file.</p>
3231 <div class="paragraph">
3232 <p>The <a href="../../example/shared_ptr_example2_test.cpp">shared_ptr_example2_test.cpp</a> sample program includes a header file,
3233 <a href="../../example/shared_ptr_example2.hpp">shared_ptr_example2.hpp</a>, which uses a <code>shared_ptr</code> to an incomplete type to hide the implementation.
3234 The instantiation of member functions which require a complete type occurs in the <a href="../../example/shared_ptr_example2.cpp">shared_ptr_example2.cpp</a>
3235 implementation file. Note that there is no need for an explicit destructor. Unlike <code>~scoped_ptr</code>, <code>~shared_ptr</code> does not require that <code>T</code> be a complete type.</p>
3239 <h3 id="shared_ptr_thread_safety">Thread Safety</h3>
3240 <div class="paragraph">
3241 <p><code>shared_ptr</code> objects offer the same level of thread safety as built-in types. A <code>shared_ptr</code> instance can be "read" (accessed using only const operations)
3242 simultaneously by multiple threads. Different <code>shared_ptr</code> instances can be "written to" (accessed using mutable operations such as <code>operator=</code> or <code>reset</code>)
3243 simultaneously by multiple threads (even when these instances are copies, and share the same reference count underneath.)</p>
3245 <div class="paragraph">
3246 <p>Any other simultaneous accesses result in undefined behavior.</p>
3248 <div class="paragraph">
3251 <div class="listingblock">
3252 <div class="content">
3253 <pre class="highlight"><code>shared_ptr<int> p(new int(42));</code></pre>
3256 <div class="listingblock">
3257 <div class="title">Code Example 4. Reading a <code>shared_ptr</code> from two threads</div>
3258 <div class="content">
3259 <pre class="highlight"><code>// thread A
3260 shared_ptr<int> p2(p); // reads p
3263 shared_ptr<int> p3(p); // OK, multiple reads are safe</code></pre>
3266 <div class="listingblock">
3267 <div class="title">Code Example 5. Writing different <code>shared_ptr</code> instances from two threads</div>
3268 <div class="content">
3269 <pre class="highlight"><code>// thread A
3270 p.reset(new int(1912)); // writes p
3273 p2.reset(); // OK, writes p2</code></pre>
3276 <div class="listingblock">
3277 <div class="title">Code Example 6. Reading and writing a <code>shared_ptr</code> from two threads</div>
3278 <div class="content">
3279 <pre class="highlight"><code>// thread A
3280 p = p3; // reads p3, writes p
3283 p3.reset(); // writes p3; undefined, simultaneous read/write</code></pre>
3286 <div class="listingblock">
3287 <div class="title">Code Example 7. Reading and destroying a <code>shared_ptr</code> from two threads</div>
3288 <div class="content">
3289 <pre class="highlight"><code>// thread A
3290 p3 = p2; // reads p2, writes p3
3293 // p2 goes out of scope: undefined, the destructor is considered a "write access"</code></pre>
3296 <div class="listingblock">
3297 <div class="title">Code Example 8. Writing a <code>shared_ptr</code> from two threads</div>
3298 <div class="content">
3299 <pre class="highlight"><code>// thread A
3300 p3.reset(new int(1));
3303 p3.reset(new int(2)); // undefined, multiple writes</code></pre>
3306 <div class="paragraph">
3307 <p>Starting with Boost release 1.33.0, <code>shared_ptr</code> uses a lock-free implementation on most common platforms.</p>
3309 <div class="paragraph">
3310 <p>If your program is single-threaded and does not link to any libraries that might have used <code>shared_ptr</code> in its default configuration,
3311 you can <code>#define</code> the macro <code>BOOST_SP_DISABLE_THREADS</code> on a project-wide basis to switch to ordinary non-atomic reference count updates.</p>
3313 <div class="paragraph">
3314 <p>(Defining <code>BOOST_SP_DISABLE_THREADS</code> in some, but not all, translation units is technically a violation of the One Definition Rule and
3315 undefined behavior. Nevertheless, the implementation attempts to do its best to accommodate the request to use non-atomic updates in those
3316 translation units. No guarantees, though.)</p>
3318 <div class="paragraph">
3319 <p>You can define the macro <code>BOOST_SP_USE_PTHREADS</code> to turn off the lock-free platform-specific implementation and fall back to the generic
3320 <code>pthread_mutex_t</code>-based code.</p>
3324 <h3 id="shared_ptr_frequently_asked_questions">Frequently Asked Questions</h3>
3325 <div class="qlist qanda">
3328 <p><em>There are several variations of shared pointers, with different tradeoffs; why does the smart pointer library supply only a single implementation? It would be useful to be able to experiment with each type so as to find the most suitable for the job at hand?</em></p>
3329 <p>An important goal of <code>shared_ptr</code> is to provide a standard shared-ownership pointer. Having a single pointer type is important for stable
3330 library interfaces, since different shared pointers typically cannot interoperate, i.e. a reference counted pointer (used by library A)
3331 cannot share ownership with a linked pointer (used by library B.)</p>
3334 <p><em>Why doesn’t shared_ptr have template parameters supplying traits or policies to allow extensive user customization?</em></p>
3335 <p>Parameterization discourages users. The <code>shared_ptr</code> template is carefully crafted to meet common needs without extensive parameterization.</p>
3338 <p><em>I am not convinced. Default parameters can be used where appropriate to hide the complexity. Again, why not policies?</em></p>
3339 <p>Template parameters affect the type. See the answer to the first question above.</p>
3342 <p><em>Why doesn’t <code>shared_ptr</code> use a linked list implementation?</em></p>
3343 <p>A linked list implementation does not offer enough advantages to offset the added cost of an extra pointer. In addition, it is expensive to
3344 make a linked list implementation thread safe.</p>
3347 <p><em>Why doesn’t <code>shared_ptr</code> (or any of the other Boost smart pointers) supply an automatic conversion to T*?</em></p>
3348 <p>Automatic conversion is believed to be too error prone.</p>
3351 <p><em>Why does <code>shared_ptr</code> supply <code>use_count()</code>?</em></p>
3352 <p>As an aid to writing test cases and debugging displays. One of the progenitors had <code>use_count()</code>, and it was useful in tracking down bugs in
3353 a complex project that turned out to have cyclic-dependencies.</p>
3356 <p><em>Why doesn’t <code>shared_ptr</code> specify complexity requirements?</em></p>
3357 <p>Because complexity requirements limit implementors and complicate the specification without apparent benefit to <code>shared_ptr</code> users. For example,
3358 error-checking implementations might become non-conforming if they had to meet stringent complexity requirements.</p>
3361 <p><em>Why doesn’t <code>shared_ptr</code> provide a <code>release()</code> function?</em></p>
3362 <p><code>shared_ptr</code> cannot give away ownership unless it’s <code>unique()</code> because the other copy will still destroy the object.</p>
3363 <div class="paragraph">
3366 <div class="listingblock">
3367 <div class="content">
3368 <pre class="highlight"><code>shared_ptr<int> a(new int);
3369 shared_ptr<int> b(a); // a.use_count() == b.use_count() == 2
3371 int * p = a.release();
3373 // Who owns p now? b will still call delete on it in its destructor.</code></pre>
3376 <div class="paragraph">
3377 <p>Furthermore, the pointer returned by <code>release()</code> would be difficult to deallocate reliably, as the source <code>shared_ptr</code> could have been created with a
3378 custom deleter, or may have pointed to an object of a different type.</p>
3382 <p><em>Why is <code>operator->()</code> const, but its return value is a non-const pointer to the element type?</em></p>
3383 <p>Shallow copy pointers, including raw pointers, typically don’t propagate constness. It makes little sense for them to do so, as you can always obtain a
3384 non-const pointer from a const one and then proceed to modify the object through it. <code>shared_ptr</code> is "as close to raw pointers as possible but no closer".</p>
3392 <h2 id="weak_ptr">weak_ptr: Non-owning Observer</h2>
3393 <div class="sectionbody">
3395 <h3 id="weak_ptr_description">Description</h3>
3396 <div class="paragraph">
3397 <p>The <code>weak_ptr</code> class template stores a "weak reference" to an object that’s already managed by a <code>shared_ptr</code>.
3398 To access the object, a <code>weak_ptr</code> can be converted to a <code>shared_ptr</code> using the <code>shared_ptr</code> constructor taking
3399 <code>weak_ptr</code>, or the <code>weak_ptr</code> member function <code>lock</code>. When the last <code>shared_ptr</code> to the object goes away and the
3400 object is deleted, the attempt to obtain a <code>shared_ptr</code> from the <code>weak_ptr</code> instances that refer to the deleted
3401 object will fail: the constructor will throw an exception of type <code>boost::bad_weak_ptr</code>, and <code>weak_ptr::lock</code> will
3402 return an empty <code>shared_ptr</code>.</p>
3404 <div class="paragraph">
3405 <p>Every <code>weak_ptr</code> meets the <code>CopyConstructible</code> and <code>Assignable</code> requirements of the C++ Standard Library, and so
3406 can be used in standard library containers. Comparison operators are supplied so that <code>weak_ptr</code> works with the standard
3407 library’s associative containers.</p>
3409 <div class="paragraph">
3410 <p><code>weak_ptr</code> operations never throw exceptions.</p>
3412 <div class="paragraph">
3413 <p>The class template is parameterized on <code>T</code>, the type of the object pointed to.</p>
3415 <div class="paragraph">
3416 <p>Compared to <code>shared_ptr</code>, <code>weak_ptr</code> provides a very limited subset of operations since accessing its stored pointer is
3417 often dangerous in multithreaded programs, and sometimes unsafe even within a single thread (that is, it may invoke undefined
3418 behavior.) Pretend for a moment that <code>weak_ptr</code> had a get member function that returned a raw pointer, and consider this innocent
3421 <div class="listingblock">
3422 <div class="content">
3423 <pre class="highlight"><code>shared_ptr<int> p(new int(5));
3424 weak_ptr<int> q(p);
3428 if(int * r = q.get())
3434 <div class="paragraph">
3435 <p>Imagine that after the <code>if</code>, but immediately before <code>r</code> is used, another thread executes the statement <code>p.reset()</code>. Now <code>r</code> is a dangling pointer.</p>
3437 <div class="paragraph">
3438 <p>The solution to this problem is to create a temporary <code>shared_ptr</code> from <code>q</code>:</p>
3440 <div class="listingblock">
3441 <div class="content">
3442 <pre class="highlight"><code>shared_ptr<int> p(new int(5));
3443 weak_ptr<int> q(p);
3447 if(shared_ptr<int> r = q.lock())
3453 <div class="paragraph">
3454 <p>Now <code>r</code> holds a reference to the object that was pointed by <code>q</code>. Even if <code>p.reset()</code> is executed in another thread, the object will stay alive until
3455 <code>r</code> goes out of scope or is reset. By obtaining a <code>shared_ptr</code> to the object, we have effectively locked it against destruction.</p>
3459 <h3 id="weak_ptr_synopsis">Synopsis</h3>
3460 <div class="paragraph">
3461 <p><code>weak_ptr</code> is defined in <code><boost/smart_ptr/weak_ptr.hpp></code>.</p>
3463 <div class="listingblock">
3464 <div class="content">
3465 <pre class="highlight"><code>namespace boost {
3467 template<class T> class weak_ptr {
3470 typedef /*see below*/ element_type;
3472 weak_ptr() noexcept;
3474 template<class Y> weak_ptr(shared_ptr<Y> const & r) noexcept;
3475 weak_ptr(weak_ptr const & r) noexcept;
3476 template<class Y> weak_ptr(weak_ptr<Y> const & r) noexcept;
3478 weak_ptr(weak_ptr && r) noexcept;
3480 template<class Y> weak_ptr(shared_ptr<Y> const & r, element_type * p) noexcept;
3481 template<class Y> weak_ptr(weak_ptr<Y> const & r, element_type * p) noexcept;
3482 template<class Y> weak_ptr(weak_ptr<Y> && r, element_type * p) noexcept;
3484 ~weak_ptr() noexcept;
3486 weak_ptr & operator=(weak_ptr const & r) noexcept;
3487 weak_ptr & operator=(weak_ptr && r) noexcept;
3488 template<class Y> weak_ptr & operator=(weak_ptr<Y> const & r) noexcept;
3489 template<class Y> weak_ptr & operator=(shared_ptr<Y> const & r) noexcept;
3491 long use_count() const noexcept;
3492 bool expired() const noexcept;
3494 bool empty() const noexcept;
3496 shared_ptr<T> lock() const noexcept;
3498 void reset() noexcept;
3500 void swap(weak_ptr<T> & b) noexcept;
3502 template<class Y> bool owner_before( weak_ptr<Y> const & r ) const noexcept;
3503 template<class Y> bool owner_before( shared_ptr<Y> const & r ) const noexcept;
3506 template<class T, class U>
3507 bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b) noexcept;
3509 template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b) noexcept;
3515 <h3 id="weak_ptr_members">Members</h3>
3517 <h4 id="weak_ptr_element_type">element_type</h4>
3518 <div class="listingblock">
3519 <div class="content">
3520 <pre class="highlight"><code>typedef ... element_type;</code></pre>
3523 <div class="paragraph">
3524 <p><code>element_type</code> is <code>T</code> when <code>T</code> is not an array type, and <code>U</code> when <code>T</code> is <code>U[]</code> or <code>U[N]</code>.</p>
3528 <h4 id="weak_ptr_constructors">constructors</h4>
3529 <div class="listingblock">
3530 <div class="content">
3531 <pre class="highlight"><code>weak_ptr() noexcept;</code></pre>
3534 <div class="ulist none">
3540 <dt class="hdlist1">Effects</dt>
3542 <p>Constructs an empty <code>weak_ptr</code>.</p>
3544 <dt class="hdlist1">Postconditions</dt>
3546 <p><code>use_count() == 0</code>.</p>
3553 <div class="listingblock">
3554 <div class="content">
3555 <pre class="highlight"><code>template<class Y> weak_ptr(shared_ptr<Y> const & r) noexcept;</code></pre>
3558 <div class="listingblock">
3559 <div class="content">
3560 <pre class="highlight"><code>weak_ptr(weak_ptr const & r) noexcept;</code></pre>
3563 <div class="listingblock">
3564 <div class="content">
3565 <pre class="highlight"><code>template<class Y> weak_ptr(weak_ptr<Y> const & r) noexcept;</code></pre>
3568 <div class="ulist none">
3574 <dt class="hdlist1">Effects</dt>
3576 <p>If <code>r</code> is empty, constructs an empty <code>weak_ptr</code>; otherwise, constructs a <code>weak_ptr</code> that shares ownership with <code>r</code> as if by storing a copy of the pointer stored in <code>r</code>.</p>
3578 <dt class="hdlist1">Postconditions</dt>
3580 <p><code>use_count() == r.use_count()</code>.</p>
3587 <div class="listingblock">
3588 <div class="content">
3589 <pre class="highlight"><code>weak_ptr(weak_ptr && r) noexcept;</code></pre>
3592 <div class="ulist none">
3598 <dt class="hdlist1">Effects</dt>
3600 <p>Constructs a <code>weak_ptr</code> that has the value <code>r</code> held.</p>
3602 <dt class="hdlist1">Postconditions</dt>
3604 <p><code>r</code> is empty.</p>
3613 <h4 id="weak_ptr_aliasing_constructors">aliasing constructors</h4>
3614 <div class="listingblock">
3615 <div class="content">
3616 <pre class="highlight"><code>template<class Y> weak_ptr(shared_ptr<Y> const & r, element_type * p) noexcept;</code></pre>
3619 <div class="listingblock">
3620 <div class="content">
3621 <pre class="highlight"><code>template<class Y> weak_ptr(weak_ptr<Y> const & r, element_type * p) noexcept;</code></pre>
3624 <div class="listingblock">
3625 <div class="content">
3626 <pre class="highlight"><code>template<class Y> weak_ptr(weak_ptr<Y> && r, element_type * p) noexcept;</code></pre>
3631 <dt class="hdlist1">Effects</dt>
3633 <p>Constructs a <code>weak_ptr</code> from <code>r</code> as if by using the corresponding converting/copy/move constructor, but stores <code>p</code> instead.</p>
3635 <dt class="hdlist1">Postconditions</dt>
3637 <p><code>use_count() == r.use_count()</code>. When <code>!expired()</code>, <code>shared_ptr<T>(*this).get() == p</code>.</p>
3641 <div class="admonitionblock note">
3645 <div class="title">Note</div>
3647 <td class="content">
3648 These constructors are an extension, not present in <code>std::weak_ptr</code>.
3655 <h4 id="weak_ptr_destructor">destructor</h4>
3656 <div class="listingblock">
3657 <div class="content">
3658 <pre class="highlight"><code>~weak_ptr() noexcept;</code></pre>
3661 <div class="ulist none">
3667 <dt class="hdlist1">Effects</dt>
3669 <p>Destroys this <code>weak_ptr</code> but has no effect on the object its stored pointer points to.</p>
3678 <h4 id="weak_ptr_assignment">assignment</h4>
3679 <div class="listingblock">
3680 <div class="content">
3681 <pre class="highlight"><code>weak_ptr & operator=(weak_ptr const & r) noexcept;</code></pre>
3684 <div class="listingblock">
3685 <div class="content">
3686 <pre class="highlight"><code>weak_ptr & operator=(weak_ptr && r) noexcept;</code></pre>
3689 <div class="listingblock">
3690 <div class="content">
3691 <pre class="highlight"><code>template<class Y> weak_ptr & operator=(weak_ptr<Y> const & r) noexcept;</code></pre>
3694 <div class="listingblock">
3695 <div class="content">
3696 <pre class="highlight"><code>template<class Y> weak_ptr & operator=(shared_ptr<Y> const & r) noexcept;</code></pre>
3699 <div class="ulist none">
3705 <dt class="hdlist1">Effects</dt>
3707 <p>Equivalent to <code>weak_ptr(r).swap(*this)</code>.</p>
3714 <div class="admonitionblock note">
3718 <div class="title">Note</div>
3720 <td class="content">
3721 The implementation is free to meet the effects (and the implied guarantees) via different means, without creating a temporary.
3728 <h4 id="weak_ptr_use_count">use_count</h4>
3729 <div class="listingblock">
3730 <div class="content">
3731 <pre class="highlight"><code>long use_count() const noexcept;</code></pre>
3734 <div class="ulist none">
3740 <dt class="hdlist1">Returns</dt>
3742 <p>0 if <code>*this</code> is empty; otherwise, the number of <code>shared_ptr</code> objects that share ownership with <code>*this</code>.</p>
3751 <h4 id="weak_ptr_expired">expired</h4>
3752 <div class="listingblock">
3753 <div class="content">
3754 <pre class="highlight"><code>bool expired() const noexcept;</code></pre>
3757 <div class="ulist none">
3763 <dt class="hdlist1">Returns</dt>
3765 <p><code>use_count() == 0</code>.</p>
3774 <h4 id="weak_ptr_empty">empty</h4>
3775 <div class="listingblock">
3776 <div class="content">
3777 <pre class="highlight"><code>bool empty() const noexcept;</code></pre>
3780 <div class="ulist none">
3786 <dt class="hdlist1">Returns</dt>
3788 <p><code>true</code> when <code>*this</code> is empty, <code>false</code> otherwise.</p>
3795 <div class="admonitionblock note">
3799 <div class="title">Note</div>
3801 <td class="content">
3802 This function is an extension, not present in <code>std::weak_ptr</code>.
3809 <h4 id="weak_ptr_lock">lock</h4>
3810 <div class="listingblock">
3811 <div class="content">
3812 <pre class="highlight"><code>shared_ptr<T> lock() const noexcept;</code></pre>
3815 <div class="ulist none">
3821 <dt class="hdlist1">Returns</dt>
3823 <p><code>expired()? shared_ptr<T>(): shared_ptr<T>(*this)</code>.</p>
3832 <h4 id="weak_ptr_reset">reset</h4>
3833 <div class="listingblock">
3834 <div class="content">
3835 <pre class="highlight"><code>void reset() noexcept;</code></pre>
3838 <div class="ulist none">
3844 <dt class="hdlist1">Effects</dt>
3846 <p>Equivalent to <code>weak_ptr().swap(*this)</code>.</p>
3855 <h4 id="weak_ptr_swap">swap</h4>
3856 <div class="listingblock">
3857 <div class="content">
3858 <pre class="highlight"><code>void swap(weak_ptr & b) noexcept;</code></pre>
3861 <div class="ulist none">
3867 <dt class="hdlist1">Effects</dt>
3869 <p>Exchanges the contents of the two smart pointers.</p>
3876 <div class="listingblock">
3877 <div class="content">
3878 <pre class="highlight"><code>template<class Y> bool owner_before( weak_ptr<Y> const & r ) const noexcept;</code></pre>
3881 <div class="listingblock">
3882 <div class="content">
3883 <pre class="highlight"><code>template<class Y> bool owner_before( shared_ptr<Y> const & r ) const noexcept;</code></pre>
3886 <div class="ulist none">
3892 <dt class="hdlist1">Returns</dt>
3894 <p>See the description of <code>operator<</code>.</p>
3904 <h3 id="weak_ptr_free_functions">Free Functions</h3>
3906 <h4 id="weak_ptr_comparison">comparison</h4>
3907 <div class="listingblock">
3908 <div class="content">
3909 <pre class="highlight"><code>template<class T, class U>
3910 bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b) noexcept;</code></pre>
3913 <div class="ulist none">
3919 <dt class="hdlist1">Returns</dt>
3921 <p>An unspecified value such that</p>
3925 <p><code>operator<</code> is a strict weak ordering as described in section [lib.alg.sorting] of the C++ standard;</p>
3928 <p>under the equivalence relation defined by <code>operator<</code>, <code>!(a < b) && !(b < a)</code>, two <code>weak_ptr</code> instances
3929 are equivalent if and only if they share ownership or are both empty.</p>
3939 <div class="admonitionblock note">
3943 <div class="title">Note</div>
3945 <td class="content">
3946 Allows <code>weak_ptr</code> objects to be used as keys in associative containers.
3953 <h4 id="weak_ptr_swap_2">swap</h4>
3954 <div class="listingblock">
3955 <div class="content">
3956 <pre class="highlight"><code>template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b) noexcept;</code></pre>
3959 <div class="ulist none">
3965 <dt class="hdlist1">Effects</dt>
3967 <p>Equivalent to <code>a.swap(b)</code>.</p>
3977 <h3 id="weak_ptr_frequently_asked_questions">Frequently Asked Questions</h3>
3978 <div class="qlist qanda">
3981 <p><em>Can an object create a weak_ptr to itself in its constructor?</em></p>
3982 <p>No. A <code>weak_ptr</code> can only be created from a <code>shared_ptr</code>, and at object construction time no
3983 <code>shared_ptr</code> to the object exists yet. Even if you could create a temporary <code>shared_ptr</code> to <code>this</code>,
3984 it would go out of scope at the end of the constructor, and all <code>weak_ptr</code> instances would instantly expire.</p>
3985 <div class="paragraph">
3986 <p>The solution is to make the constructor private, and supply a factory function that returns a <code>shared_ptr</code>:</p>
3988 <div class="listingblock">
3989 <div class="content">
3990 <pre class="highlight"><code>class X
3998 static shared_ptr<X> create()
4000 shared_ptr<X> px(new X);
4001 // create weak pointers from px here
4014 <h2 id="make_shared">make_shared: Creating shared_ptr</h2>
4015 <div class="sectionbody">
4017 <h3 id="make_shared_description">Description</h3>
4018 <div class="paragraph">
4019 <p>The function templates <code>make_shared</code> and <code>allocate_shared</code> provide convenient,
4020 safe and efficient ways to create <code>shared_ptr</code> objects.</p>
4024 <h3 id="make_shared_rationale">Rationale</h3>
4025 <div class="paragraph">
4026 <p>Consistent use of <code>shared_ptr</code> can eliminate the need to use an explicit
4027 <code>delete</code>, but alone it provides no support in avoiding explicit <code>new</code>. There
4028 were repeated requests from users for a factory function that creates an
4029 object of a given type and returns a <code>shared_ptr</code> to it. Besides convenience
4030 and style, such a function is also exception safe and considerably faster
4031 because it can use a single allocation for both the object and its
4032 corresponding control block, eliminating a significant portion of
4033 <code>shared_ptr</code> construction overhead. This eliminates one of the major
4034 efficiency complaints about <code>shared_ptr</code>.</p>
4036 <div class="paragraph">
4037 <p>The family of overloaded function templates, <code>make_shared</code> and
4038 <code>allocate_shared</code>, were provided to address this need. <code>make_shared</code> uses the
4039 global <code>operator new</code> to allocate memory, whereas <code>allocate_shared</code> uses an
4040 user-supplied allocator, allowing finer control.</p>
4042 <div class="paragraph">
4043 <p>The rationale for choosing the name <code>make_shared</code> is that the expression
4044 <code>make_shared<Widget>()</code> can be read aloud and conveys the intended meaning.</p>
4046 <div class="paragraph">
4047 <p>Originally the Boost function templates <code>allocate_shared</code> and <code>make_shared</code>
4048 were provided for scalar objects only. There was a need to have efficient
4049 allocation of array objects. One criticism of class template <code>shared_array</code>
4050 was always the lack of a utility like <code>make_shared</code> that uses only a single
4051 allocation. When <code>shared_ptr</code> was enhanced to support array types, additional
4052 overloads of <code>allocate_shared</code> and <code>make_shared</code> were provided for array
4057 <h3 id="make_shared_synopsis">Synopsis</h3>
4058 <div class="paragraph">
4059 <p><code>make_shared</code> and <code>allocate_shared</code> are defined in
4060 <code><boost/smart_ptr/make_shared.hpp></code>.</p>
4062 <div class="listingblock">
4063 <div class="content">
4064 <pre class="highlight"><code>namespace boost {
4065 <code>// T is not an array</code>
4066 template<class T, class... Args>
4067 shared_ptr<T> make_shared(Args&&... args);
4068 template<class T, class A, class... Args>
4069 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
4071 <code>// T is an array of unknown bounds</code>
4072 template<class T>
4073 shared_ptr<T> make_shared(std::size_t n);
4074 template<class T, class A>
4075 shared_ptr<T> allocate_shared(const A& a, std::size_t n);
4077 <code>// T is an array of known bounds</code>
4078 template<class T>
4079 shared_ptr<T> make_shared();
4080 template<class T, class A>
4081 shared_ptr<T> allocate_shared(const A& a);
4083 <code>// T is an array of unknown bounds</code>
4084 template<class T> shared_ptr<T>
4085 make_shared(std::size_t n, const remove_extent_t<T>& v);
4086 template<class T, class A> shared_ptr<T>
4087 allocate_shared(const A& a, std::size_t n, const remove_extent_t<T>& v);
4089 <code>// T is an array of known bounds</code>
4090 template<class T>
4091 shared_ptr<T> make_shared(const remove_extent_t<T>& v);
4092 template<class T, class A>
4093 shared_ptr<T> allocate_shared(const A& a, const remove_extent_t<T>& v);
4095 <code>// T is not an array of unknown bounds</code>
4096 template<class T>
4097 shared_ptr<T> make_shared_noinit();
4098 template<class T, class A>
4099 shared_ptr<T> allocate_shared_noinit(const A& a);
4101 <code>// T is an array of unknown bounds</code>
4102 template<class T>
4103 shared_ptr<T> make_shared_noinit(std::size_t n);
4104 template<class T, class A>
4105 shared_ptr<T> allocate_shared_noinit(const A& a, std::size_t n);
4111 <h3 id="make_shared_common_requirements">Common Requirements</h3>
4112 <div class="paragraph">
4113 <p>The common requirements that apply to all <code>make_shared</code> and <code>allocate_shared</code>
4114 overloads, unless specified otherwise, are described below.</p>
4118 <dt class="hdlist1">Requires</dt>
4120 <p><code>A</code> shall be an <em>allocator</em>. The copy constructor and destructor
4121 of <code>A</code> shall not throw exceptions.</p>
4123 <dt class="hdlist1">Effects</dt>
4125 <p>Allocates memory for an object of type <code>T</code> or <code>n</code> objects of <code>U</code>
4126 (if <code>T</code> is an array type of the form <code>U[]</code> and <code>n</code> is determined by
4127 arguments, as specified by the concrete overload). The object is initialized
4128 from arguments as specified by the concrete overload. Uses a rebound copy of
4129 <code>a</code> (for an unspecified <code>value_type</code>) to allocate memory. If an exception is
4130 thrown, the functions have no effect.</p>
4132 <dt class="hdlist1">Returns</dt>
4134 <p>A <code>shared_ptr</code> instance that stores and owns the address of the
4135 newly constructed object.</p>
4137 <dt class="hdlist1">Postconditions</dt>
4139 <p><code>r.get() != 0</code> and <code>r.use_count() == 1</code>, where <code>r</code>
4140 is the return value.</p>
4142 <dt class="hdlist1">Throws</dt>
4144 <p><code>std::bad_alloc</code>, an exception thrown from <code>A::allocate</code>, or from the
4145 initialization of the object.</p>
4147 <dt class="hdlist1">Remarks</dt>
4152 <p>Performs no more than one memory allocation. This provides efficiency
4153 equivalent to an intrusive smart pointer.</p>
4156 <p>When an object of an array type is specified to be initialized to a value of
4157 the same type <code>v</code>, this shall be interpreted to mean that each array element
4158 of the object is initialized to the corresponding element from <code>v</code>.</p>
4161 <p>When an object of an array type is specified to be value-initialized, this
4162 shall be interpreted to mean that each array element of the object is
4163 value-initialized.</p>
4166 <p>When a (sub)object of non-array type <code>U</code> is specified to be initialized to
4167 a value <code>v</code>, or constructed from <code>args...</code>, <code>make_shared</code> shall perform
4168 this initialization via the expression <code>::new(p) U(expr)</code> (where
4169 <code><em>expr</em></code> is <code>v</code> or <code>std::forward<Args>(args)...)</code> respectively) and <code>p</code>
4170 has type <code>void*</code> and points to storage suitable to hold an object of type
4174 <p>When a (sub)object of non-array type <code>U</code> is specified to be initialized to
4175 a value <code>v</code>, or constructed from <code>args...</code>, <code>allocate_shared</code> shall
4176 perform this initialization via the expression
4177 <code>std::allocator_traits<A2>::construct(a2, p, expr)</code> (where
4178 <code><em>expr</em></code> is <code>v</code> or <code>std::forward<Args>(args)...)</code> respectively), <code>p</code>
4179 points to storage suitable to hold an object of type <code>U</code>, and <code>a2</code> of
4180 type <code>A2</code> is a potentially rebound copy of <code>a</code>.</p>
4183 <p>When a (sub)object of non-array type <code>U</code> is specified to be
4184 default-initialized, <code>make_shared_noinit</code> and <code>allocate_shared_noinit</code> shall
4185 perform this initialization via the expression <code>::new(p) U</code>, where
4186 <code>p</code> has type <code>void*</code> and points to storage suitable to hold an object of
4187 type <code>U</code>.</p>
4190 <p>When a (sub)object of non-array type <code>U</code> is specified to be
4191 value-initialized, <code>make_shared</code> shall perform this initialization via the
4192 expression <code>::new(p) U()</code>, where <code>p</code> has type <code>void*</code> and points to
4193 storage suitable to hold an object of type <code>U</code>.</p>
4196 <p>When a (sub)object of non-array type <code>U</code> is specified to be
4197 value-initialized, <code>allocate_shared</code> shall perform this initialization via the
4198 expression <code>std::allocator_traits<A2>::construct(a2, p)</code>, where
4199 <code>p</code> points to storage suitable to hold an object of type <code>U</code> and <code>a2</code> of
4200 type <code>A2</code> is a potentially rebound copy of <code>a</code>.</p>
4203 <p>Array elements are initialized in ascending order of their addresses.</p>
4206 <p>When the lifetime of the object managed by the return value ends, or when
4207 the initialization of an array element throws an exception, the initialized
4208 elements should be destroyed in the reverse order of their construction.</p>
4215 <div class="admonitionblock note">
4219 <div class="title">Note</div>
4221 <td class="content">
4222 These functions will typically allocate more memory than the total size
4223 of the element objects to allow for internal bookkeeping structures such as
4224 the reference counts.
4231 <h3 id="make_shared_free_functions">Free Functions</h3>
4232 <div class="listingblock">
4233 <div class="content">
4234 <pre class="highlight"><code>template<class T, class... Args>
4235 shared_ptr<T> make_shared(Args&&... args);</code></pre>
4238 <div class="listingblock">
4239 <div class="content">
4240 <pre class="highlight"><code>template<class T, class A, class... Args>
4241 shared_ptr<T> allocate_shared(const A& a, Args&&... args);</code></pre>
4244 <div class="ulist none">
4250 <dt class="hdlist1">Constraints</dt>
4252 <p><code>T</code> is not an array.</p>
4254 <dt class="hdlist1">Returns</dt>
4256 <p>A <code>shared_ptr</code> to an object of type <code>T</code>, constructed from
4257 <code>args...</code>.</p>
4259 <dt class="hdlist1">Examples</dt>
4264 <p><code>auto p = make_shared<int>();</code></p>
4267 <p><code>auto p = make_shared<std::vector<int> >(16, 1);</code></p>
4271 <div class="listingblock">
4272 <div class="content">
4273 <pre class="highlight"><code>template<class T>
4274 shared_ptr<T> make_shared(std::size_t n);</code></pre>
4277 <div class="listingblock">
4278 <div class="content">
4279 <pre class="highlight"><code>template<class T, class A>
4280 shared_ptr<T> allocate_shared(const A& a, std::size_t n);</code></pre>
4283 <div class="ulist none">
4289 <dt class="hdlist1">Constraints</dt>
4291 <p><code>T</code> is an array of unknown bounds.</p>
4293 <dt class="hdlist1">Returns</dt>
4295 <p>A <code>shared_ptr</code> to a sequence of <code>n</code> value-initialized objects of
4296 type <code>remove_extent_t<T></code>.</p>
4298 <dt class="hdlist1">Examples</dt>
4303 <p><code>auto p = make_shared<double[]>(1024);</code></p>
4306 <p><code>auto p = make_shared<double[][2][2]>(6);</code></p>
4310 <div class="listingblock">
4311 <div class="content">
4312 <pre class="highlight"><code>template<class T>
4313 shared_ptr<T> make_shared();</code></pre>
4316 <div class="listingblock">
4317 <div class="content">
4318 <pre class="highlight"><code>template<class T, class A>
4319 shared_ptr<T> allocate_shared(const A& a);</code></pre>
4322 <div class="ulist none">
4328 <dt class="hdlist1">Constraints</dt>
4330 <p><code>T</code> is an array of known bounds.</p>
4332 <dt class="hdlist1">Returns</dt>
4334 <p>A <code>shared_ptr</code> to a sequence of <code>extent_v<T></code> value-initialized
4335 objects of type <code>remove_extent_t<T></code>.</p>
4337 <dt class="hdlist1">Examples</dt>
4342 <p><code>auto p = make_shared<double[1024]>();</code></p>
4345 <p><code>auto p = make_shared<double[6][2][2]>();</code></p>
4349 <div class="listingblock">
4350 <div class="content">
4351 <pre class="highlight"><code>template<class T> shared_ptr<T>
4352 make_shared(std::size_t n, const remove_extent_t<T>& v);</code></pre>
4355 <div class="listingblock">
4356 <div class="content">
4357 <pre class="highlight"><code>template<class T, class A> shared_ptr<T>
4358 allocate_shared(const A& a, std::size_t n, const remove_extent_t<T>& v);</code></pre>
4361 <div class="ulist none">
4367 <dt class="hdlist1">Constraints</dt>
4369 <p><code>T</code> is an array of unknown bounds.</p>
4371 <dt class="hdlist1">Returns</dt>
4373 <p>A <code>shared_ptr</code> to a sequence of <code>n</code> objects of type
4374 <code>remove_extent_t<T></code>, each initialized to <code>v</code>.</p>
4376 <dt class="hdlist1">Examples</dt>
4381 <p><code>auto p = make_shared<double[]>(1024, 1.0);</code></p>
4384 <p><code>auto p = make_shared<double[][2]>(6, {1.0, 0.0});</code></p>
4387 <p><code>auto p = make_shared<std::vector<int>[]>(4, {1, 2});</code></p>
4391 <div class="listingblock">
4392 <div class="content">
4393 <pre class="highlight"><code>template<class T>
4394 shared_ptr<T> make_shared(const remove_extent_t<T>& v);</code></pre>
4397 <div class="listingblock">
4398 <div class="content">
4399 <pre class="highlight"><code>template<class T, class A>
4400 shared_ptr<T> allocate_shared(const A& a, const remove_extent_t<T>& v);</code></pre>
4403 <div class="ulist none">
4409 <dt class="hdlist1">Constraints</dt>
4411 <p><code>T</code> is an array of known bounds.</p>
4413 <dt class="hdlist1">Returns</dt>
4415 <p>A <code>shared_ptr</code> to a sequence of <code>extent_v<T></code> objects of type
4416 <code>remove_extent_t<T></code>, each initialized to <code>v</code>.</p>
4418 <dt class="hdlist1">Examples</dt>
4423 <p><code>auto p = make_shared<double[1024]>(1.0);</code></p>
4426 <p><code>auto p = make_shared<double[6][2]>({1.0, 0.0});</code></p>
4429 <p><code>auto p = make_shared<std::vector<int>[4]>({1, 2});</code></p>
4433 <div class="listingblock">
4434 <div class="content">
4435 <pre class="highlight"><code>template<class T>
4436 shared_ptr<T> make_shared_noinit();</code></pre>
4439 <div class="listingblock">
4440 <div class="content">
4441 <pre class="highlight"><code>template<class T, class A>
4442 shared_ptr<T> allocate_shared_noinit(const A& a);</code></pre>
4445 <div class="ulist none">
4451 <dt class="hdlist1">Constraints</dt>
4453 <p><code>T</code> is not an array, or is an array of known bounds.</p>
4455 <dt class="hdlist1">Returns</dt>
4457 <p>A <code>shared_ptr</code> to a default-initialized object of type <code>T</code>, or a
4458 sequence of <code>extent_v<T></code> default-initialized objects of type
4459 <code>remove_extent_t<T></code>, respectively.</p>
4461 <dt class="hdlist1">Example</dt>
4463 <p><code>auto p = make_shared_noinit<double[1024]>();</code></p>
4470 <div class="listingblock">
4471 <div class="content">
4472 <pre class="highlight"><code>template<class T>
4473 shared_ptr<T> make_shared_noinit(std::size_t n);</code></pre>
4476 <div class="listingblock">
4477 <div class="content">
4478 <pre class="highlight"><code>template<class T, class A>
4479 shared_ptr<T> allocate_shared_noinit(const A& a, std::size_t n);</code></pre>
4482 <div class="ulist none">
4488 <dt class="hdlist1">Constraints</dt>
4490 <p><code>T</code> is an array of unknown bounds.</p>
4492 <dt class="hdlist1">Returns</dt>
4494 <p>A <code>shared_ptr</code> to a sequence of <code><em>n</em></code> default-initialized objects
4495 of type <code>remove_extent_t<T></code>.</p>
4497 <dt class="hdlist1">Example</dt>
4499 <p><code>auto p = make_shared_noinit<double[]>(1024);</code></p>
4510 <h2 id="enable_shared_from_this">enable_shared_from_this</h2>
4511 <div class="sectionbody">
4513 <h3 id="enable_shared_from_this_description">Description</h3>
4514 <div class="paragraph">
4515 <p>The class template <code>enable_shared_from_this</code> is used as a base class that allows
4516 a <code>shared_ptr</code> or a <code>weak_ptr</code> to the current object to be obtained from within a
4517 member function.</p>
4519 <div class="paragraph">
4520 <p><code>enable_shared_from_this<T></code> defines two member functions called <code>shared_from_this</code>
4521 that return a <code>shared_ptr<T></code> and <code>shared_ptr<T const></code>, depending on constness, to
4522 <code>this</code>. It also defines two member functions called <code>weak_from_this</code> that return a
4523 corresponding <code>weak_ptr</code>.</p>
4527 <h3 id="enable_shared_from_this_example">Example</h3>
4528 <div class="listingblock">
4529 <div class="content">
4530 <pre class="highlight"><code>#include <boost/enable_shared_from_this.hpp>
4531 #include <boost/shared_ptr.hpp>
4532 #include <cassert>
4534 class Y: public boost::enable_shared_from_this<Y>
4538 boost::shared_ptr<Y> f()
4540 return shared_from_this();
4546 boost::shared_ptr<Y> p(new Y);
4547 boost::shared_ptr<Y> q = p->f();
4549 assert(!(p < q || q < p)); // p and q must share ownership
4555 <h3 id="enable_shared_from_this_synopsis">Synopsis</h3>
4556 <div class="paragraph">
4557 <p><code>enable_shared_from_this</code> is defined in <code><boost/smart_ptr/enable_shared_from_this.hpp></code>.</p>
4559 <div class="listingblock">
4560 <div class="content">
4561 <pre class="highlight"><code>namespace boost {
4563 template<class T> class enable_shared_from_this {
4567 weak_ptr<T> weak_this_;
4571 enable_shared_from_this() = default;
4572 ~enable_shared_from_this() = default;
4574 enable_shared_from_this(const enable_shared_from_this&) noexcept;
4575 enable_shared_from_this& operator=(const enable_shared_from_this&) noexcept;
4579 shared_ptr<T> shared_from_this();
4580 shared_ptr<T const> shared_from_this() const;
4582 weak_ptr<T> weak_from_this() noexcept;
4583 weak_ptr<T const> weak_from_this() const noexcept;
4590 <h3 id="enable_shared_from_this_members">Members</h3>
4591 <div class="listingblock">
4592 <div class="content">
4593 <pre class="highlight"><code>enable_shared_from_this(enable_shared_from_this const &) noexcept;</code></pre>
4596 <div class="ulist none">
4602 <dt class="hdlist1">Effects</dt>
4604 <p>Default-constructs <code>weak_this_</code>.</p>
4611 <div class="admonitionblock note">
4615 <div class="title">Note</div>
4617 <td class="content">
4618 <code>weak_this_</code> is <em>not</em> copied from the argument.
4623 <div class="listingblock">
4624 <div class="content">
4625 <pre class="highlight"><code>enable_shared_from_this& operator=(enable_shared_from_this const &) noexcept;</code></pre>
4628 <div class="ulist none">
4634 <dt class="hdlist1">Returns</dt>
4636 <p><code>*this</code>.</p>
4643 <div class="admonitionblock note">
4647 <div class="title">Note</div>
4649 <td class="content">
4650 <code>weak_this_</code> is unchanged.
4655 <div class="listingblock">
4656 <div class="content">
4657 <pre class="highlight"><code>template<class T> shared_ptr<T> shared_from_this();</code></pre>
4660 <div class="listingblock">
4661 <div class="content">
4662 <pre class="highlight"><code>template<class T> shared_ptr<T const> shared_from_this() const;</code></pre>
4665 <div class="ulist none">
4671 <dt class="hdlist1">Returns</dt>
4673 <p><code>shared_ptr<T>(weak_this_)</code>.</p>
4680 <div class="admonitionblock note">
4684 <div class="title">Note</div>
4686 <td class="content">
4687 These members throw <code>bad_weak_ptr</code> when <code>*this</code> is not owned by a <code>shared_ptr</code>.
4692 <div class="admonitionblock note">
4696 <div class="title">Note</div>
4698 <td class="content">
4699 <div class="paragraph">
4700 <p><code>weak_this_</code> is initialized by <code>shared_ptr</code> to a copy of itself when it’s constructed by a pointer to <code>*this</code>.
4701 For example, in the following code:</p>
4703 <div class="listingblock">
4704 <div class="content">
4705 <pre class="highlight"><code>class Y: public boost::enable_shared_from_this<Y> {};
4709 boost::shared_ptr<Y> p(new Y);
4713 <div class="paragraph">
4714 <p>the construction of <code>p</code> will automatically initialize <code>p->weak_this_</code> to <code>p</code>.</p>
4720 <div class="listingblock">
4721 <div class="content">
4722 <pre class="highlight"><code>template<class T> weak_ptr<T> weak_from_this() noexcept;</code></pre>
4725 <div class="listingblock">
4726 <div class="content">
4727 <pre class="highlight"><code>template<class T> weak_ptr<T const> weak_from_this() const noexcept;</code></pre>
4730 <div class="ulist none">
4736 <dt class="hdlist1">Returns</dt>
4738 <p><code>weak_this_</code>.</p>
4745 <div class="admonitionblock note">
4749 <div class="title">Note</div>
4751 <td class="content">
4752 Unlike <code>shared_from_this()</code>, <code>weak_from_this()</code> is valid in a destructor
4753 and returns a <code>weak_ptr</code> that is <code>expired()</code> but still shares ownership
4754 with other <code>weak_ptr</code> instances (if any) that refer to the object.
4763 <h2 id="enable_shared_from">enable_shared_from</h2>
4764 <div class="sectionbody">
4766 <h3 id="enable_shared_from_description">Description</h3>
4767 <div class="paragraph">
4768 <p><code>enable_shared_from</code> is used as a base class that allows a <code>shared_ptr</code> or a
4769 <code>weak_ptr</code> to be obtained given a raw pointer to the object, by using the
4770 functions <code>shared_from</code> and <code>weak_from</code>.</p>
4772 <div class="paragraph">
4773 <p><code>enable_shared_from</code> differs from <code>enable_shared_from_this<T></code> by the fact
4774 that it’s not a template, and is its recommended replacement for new code.</p>
4778 <h3 id="enable_shared_from_example">Example</h3>
4779 <div class="listingblock">
4780 <div class="content">
4781 <pre class="highlight"><code>#include <boost/smart_ptr/enable_shared_from.hpp>
4782 #include <boost/shared_ptr.hpp>
4783 #include <cassert>
4785 class Y: public boost::enable_shared_from
4789 boost::shared_ptr<Y> f()
4791 return boost::shared_from( this );
4797 boost::shared_ptr<Y> p(new Y);
4798 boost::shared_ptr<Y> q = p->f();
4800 assert(!(p < q || q < p)); // p and q must share ownership
4806 <h3 id="enable_shared_from_synopsis">Synopsis</h3>
4807 <div class="paragraph">
4808 <p><code>enable_shared_from</code> is defined in <code><boost/smart_ptr/enable_shared_from.hpp></code>.</p>
4810 <div class="listingblock">
4811 <div class="content">
4812 <pre class="highlight"><code>namespace boost {
4814 class enable_shared_from: public enable_shared_from_this<enable_shared_from>
4818 template<class T> shared_ptr<T> shared_from( T * p );
4819 template<class T> weak_ptr<T> weak_from( T * p ) noexcept;
4825 <h3 id="enable_shared_from_functions">Functions</h3>
4826 <div class="listingblock">
4827 <div class="content">
4828 <pre class="highlight"><code>template<class T> shared_ptr<T> shared_from( T * p );</code></pre>
4831 <div class="ulist none">
4837 <dt class="hdlist1">Returns</dt>
4839 <p><code>shared_ptr<T>( p->enable_shared_from::shared_from_this(), p )</code>.</p>
4846 <div class="admonitionblock note">
4850 <div class="title">Note</div>
4852 <td class="content">
4853 Throws <code>bad_weak_ptr</code> when <code>p</code> is not owned by a <code>shared_ptr</code>.
4858 <div class="listingblock">
4859 <div class="content">
4860 <pre class="highlight"><code>template<class T> weak_ptr<T> weak_from( T * p ) noexcept;</code></pre>
4863 <div class="ulist none">
4869 <dt class="hdlist1">Returns</dt>
4871 <p><code>weak_ptr<T>( p->enable_shared_from::weak_from_this(), p )</code>.</p>
4878 <div class="admonitionblock note">
4882 <div class="title">Note</div>
4884 <td class="content">
4885 Unlike <code>shared_from(this)</code>, <code>weak_from(this)</code> is valid in a destructor
4886 and returns a <code>weak_ptr</code> that is <code>expired()</code> but still shares ownership
4887 with other <code>weak_ptr</code> instances (if any) that refer to the object.
4896 <h2 id="make_unique">make_unique: Creating unique_ptr</h2>
4897 <div class="sectionbody">
4899 <h3 id="make_unique_description">Description</h3>
4900 <div class="paragraph">
4901 <p>The <code>make_unique</code> function templates provide convenient and safe ways to
4902 create <code>std::unique_ptr</code> objects.</p>
4906 <h3 id="make_unique_rationale">Rationale</h3>
4907 <div class="paragraph">
4908 <p>The C++11 standard introduced <code>std::unique_ptr</code> but did not provide any
4909 <code>make_unique</code> utility like <code>std::make_shared</code> that provided the same
4910 exception safety and facility to avoid writing <code>new</code> expressions. Before it
4911 was implemented by some standard library vendors (and prior to the C++14
4912 standard introducing <code>std::make_unique</code>), this library provided it due to
4913 requests from users.</p>
4915 <div class="paragraph">
4916 <p>This library also provides additional overloads of <code>make_unique</code> for
4917 default-initialization, when users do not need or want to incur the expense
4918 of value-initialization. The C++ standard does not yet provide this
4919 feature with <code>std::make_unique</code>.</p>
4923 <h3 id="make_unique_synopsis">Synopsis</h3>
4924 <div class="paragraph">
4925 <p><code>make_unique</code> is defined in <code><boost/smart_ptr/make_unique.hpp></code>.</p>
4927 <div class="listingblock">
4928 <div class="content">
4929 <pre class="highlight"><code>namespace boost {
4930 <code>// T is not an array</code>
4931 template<class T, class... Args>
4932 std::unique_ptr<T> make_unique(Args&&... args);
4934 <code>// T is not an array</code>
4935 template<class T>
4936 std::unique_ptr<T> make_unique(type_identity_t<T>&& v);
4938 <code>// T is an array of unknown bounds</code>
4939 template<class T>
4940 std::unique_ptr<T> make_unique(std::size_t n);
4942 <code>// T is not an array</code>
4943 template<class T>
4944 std::unique_ptr<T> make_unique_noinit();
4946 <code>// T is an array of unknown bounds</code>
4947 template<class T>
4948 std::unique_ptr<T> make_unique_noinit(std::size_t n);
4954 <h3 id="make_unique_free_functions">Free Functions</h3>
4955 <div class="listingblock">
4956 <div class="content">
4957 <pre class="highlight"><code>template<class T, class... Args>
4958 std::unique_ptr<T> make_unique(Args&&... args);</code></pre>
4961 <div class="ulist none">
4967 <dt class="hdlist1">Constraints</dt>
4969 <p><code>T</code> is not an array.</p>
4971 <dt class="hdlist1">Returns</dt>
4973 <p><code>std::unique_ptr<T>(new T(std::forward<Args>(args)...)</code>.</p>
4975 <dt class="hdlist1">Example</dt>
4977 <p><code>auto p = make_unique<int>();</code></p>
4984 <div class="listingblock">
4985 <div class="content">
4986 <pre class="highlight"><code>template<class T>
4987 std::unique_ptr<T> make_unique(type_identity_t<T>&& v);</code></pre>
4990 <div class="ulist none">
4996 <dt class="hdlist1">Constraints</dt>
4998 <p><code>T</code> is not an array.</p>
5000 <dt class="hdlist1">Returns</dt>
5002 <p><code>std::unique_ptr<T>(new T(std::move(v))</code>.</p>
5004 <dt class="hdlist1">Example</dt>
5006 <p><code>auto p = make_unique<std::vector<int> >({1, 2});</code></p>
5013 <div class="listingblock">
5014 <div class="content">
5015 <pre class="highlight"><code>template<class T>
5016 std::unique_ptr<T> make_unique(std::size_t n);</code></pre>
5019 <div class="ulist none">
5025 <dt class="hdlist1">Constraints</dt>
5027 <p><code>T</code> is an array of unknown bounds.</p>
5029 <dt class="hdlist1">Returns</dt>
5031 <p><code>std::unique_ptr<T>(new remove_extent_t<T>[n]())</code>.</p>
5033 <dt class="hdlist1">Example</dt>
5035 <p><code>auto p = make_unique<double[]>(1024);</code></p>
5042 <div class="listingblock">
5043 <div class="content">
5044 <pre class="highlight"><code>template<class T>
5045 std::unique_ptr<T> make_unique_noinit();</code></pre>
5048 <div class="ulist none">
5054 <dt class="hdlist1">Constraints</dt>
5056 <p><code>T</code> is not an array.</p>
5058 <dt class="hdlist1">Returns</dt>
5060 <p><code>std::unique_ptr<T>(new T)</code>.</p>
5062 <dt class="hdlist1">Example</dt>
5064 <p><code>auto p = make_unique_noinit<double[1024]>();</code></p>
5071 <div class="listingblock">
5072 <div class="content">
5073 <pre class="highlight"><code>template<class T>
5074 std::unique_ptr<T> make_unique_noinit(std::size_t n);</code></pre>
5077 <div class="ulist none">
5083 <dt class="hdlist1">Constraints</dt>
5085 <p><code>T</code> is an array of unknown bounds.</p>
5087 <dt class="hdlist1">Returns</dt>
5089 <p><code>std::unique_ptr<T>(new remove_extent_t<T>[n])</code>.</p>
5091 <dt class="hdlist1">Example</dt>
5093 <p><code>auto p = make_unique_noinit<double[]>(1024);</code></p>
5104 <h2 id="allocate_unique">allocate_unique: Creating unique_ptr</h2>
5105 <div class="sectionbody">
5107 <h3 id="allocate_unique_description">Description</h3>
5108 <div class="paragraph">
5109 <p>The <code>allocate_unique</code> family of function templates provide convenient and safe
5110 ways to obtain a <code>std::unique_ptr</code> that manages a new object created using an
5115 <h3 id="allocate_unique_rationale">Rationale</h3>
5116 <div class="paragraph">
5117 <p>The C++14 standard introduced <code>std::make_unique</code> which used operator <code>new</code> to
5118 create new objects. However, there is no convenient facility in the standard
5119 library to use an allocator for the creation of the objects managed by
5120 <code>std::unique_ptr</code>. Users writing allocator aware code have often requested an
5121 <code>allocate_unique</code> factory function. This function is to <code>std::unique_ptr</code> what
5122 <code>std::allocate_shared</code> is to <code>std::shared_ptr</code>.</p>
5126 <h3 id="allocate_unique_synopsis">Synopsis</h3>
5127 <div class="paragraph">
5128 <p><code>allocate_unique</code> is defined in <code><boost/smart_ptr/allocate_unique.hpp></code>.</p>
5130 <div class="listingblock">
5131 <div class="content">
5132 <pre class="highlight"><code>namespace boost {
5133 template<class T, class A>
5134 class alloc_deleter;
5136 template<class T, class A>
5137 using alloc_noinit_deleter = alloc_deleter<T, noinit_adaptor<A>>;
5139 <code>// T is not an array</code>
5140 template<class T, class A, class... Args>
5141 std::unique_ptr<T, alloc_deleter<T, A>>
5142 allocate_unique(const A& a, Args&&... args);
5144 <code>// T is not an array</code>
5145 template<class T, class A>
5146 std::unique_ptr<T, alloc_deleter<T, A>>
5147 allocate_unique(const A& a, type_identity_t<T>&& v);
5149 <code>// T is an array of unknown bounds</code>
5150 template<class T, class A>
5151 std::unique_ptr<T, alloc_deleter<T, A>>
5152 allocate_unique(const A& a, std::size_t n);
5154 <code>// T is an array of known bounds</code>
5155 template<class T, class A>
5156 std::unique_ptr<remove_extent_t<T>[], alloc_deleter<T, A>>
5157 allocate_unique(const A& a);
5159 <code>// T is an array of unknown bounds</code>
5160 template<class T, class A>
5161 std::unique_ptr<T, alloc_deleter<T, A>>
5162 allocate_unique(const A& a, std::size_t n, const remove_extent_t<T>& v);
5164 <code>// T is an array of known bounds</code>
5165 template<class T, class A>
5166 std::unique_ptr<remove_extent_t<T>[], alloc_deleter<T, A>>
5167 allocate_unique(const A& a, const remove_extent_t<T>& v);
5169 <code>// T is not an array</code>
5170 template<class T, class A>
5171 std::unique_ptr<T, alloc_noinit_deleter<T, A>>
5172 allocate_unique_noinit(const A& a);
5174 <code>// T is an array of unknown bounds</code>
5175 template<class T, class A>
5176 std::unique_ptr<T, alloc_noinit_deleter<T, A>>
5177 allocate_unique_noinit(const A& a, std::size_t n);
5179 <code>// T is an array of known bounds</code>
5180 template<class T, class A>
5181 std::unique_ptr<remove_extent_t<T>[], alloc_noinit_deleter<T, A>>
5182 allocate_unique_noinit(const A& a);
5188 <h3 id="allocate_unique_common_requirements">Common Requirements</h3>
5189 <div class="paragraph">
5190 <p>The common requirements that apply to all <code>allocate_unique</code> and
5191 <code>allocate_unique_noinit</code> overloads, unless specified otherwise, are described
5196 <dt class="hdlist1">Requires</dt>
5198 <p><code>A</code> shall be an <em>allocator</em>. The copy constructor and destructor
5199 of <code>A</code> shall not throw exceptions.</p>
5201 <dt class="hdlist1">Effects</dt>
5203 <p>Allocates memory for an object of type <code>T</code> or <code>n</code> objects of <code>U</code>
5204 (if <code>T</code> is an array type of the form <code>U[]</code> and <code>n</code> is determined by
5205 arguments, as specified by the concrete overload). The object is initialized
5206 from arguments as specified by the concrete overload. Uses a rebound copy of
5207 <code>a</code> (for an unspecified <code>value_type</code>) to allocate memory. If an exception is
5208 thrown, the functions have no effect.</p>
5210 <dt class="hdlist1">Returns</dt>
5212 <p>A <code>std::unique_ptr</code> instance that stores and owns the address of the
5213 newly constructed object.</p>
5215 <dt class="hdlist1">Postconditions</dt>
5217 <p><code>r.get() != 0</code>, where <code>r</code> is the return value.</p>
5219 <dt class="hdlist1">Throws</dt>
5221 <p>An exception thrown from <code>A::allocate</code>, or from the initialization of
5224 <dt class="hdlist1">Remarks</dt>
5229 <p>When an object of an array type is specified to be initialized to a value of
5230 the same type <code>v</code>, this shall be interpreted to mean that each array element
5231 of the object is initialized to the corresponding element from <code>v</code>.</p>
5234 <p>When an object of an array type is specified to be value-initialized, this
5235 shall be interpreted to mean that each array element of the object is
5236 value-initialized.</p>
5239 <p>When a (sub)object of non-array type <code>U</code> is specified to be initialized to a
5240 value <code>v</code>, or constructed from <code>args...</code>, <code>allocate_unique</code> shall perform this
5241 initialization via the expression
5242 <code>std::allocator_traits<A2>::construct(a2, p, expr)</code> (where <code><em>expr</em></code> is <code>v</code> or
5243 <code>std::forward<Args>(args)...)</code> respectively), <code>p</code> points to storage suitable
5244 to hold an object of type <code>U</code>, and <code>a2</code> of type <code>A2</code> is a potentially rebound
5245 copy of <code>a</code>.</p>
5248 <p>When a (sub)object of non-array type <code>U</code> is specified to be
5249 default-initialized, <code>allocate_unique_noinit</code> shall perform this initialization
5250 via the expression <code>::new(p) U</code>, where <code>p</code> has type <code>void*</code> and points to
5251 storage suitable to hold an object of type <code>U</code>.</p>
5254 <p>When a (sub)object of non-array type <code>U</code> is specified to be
5255 value-initialized, <code>allocate_unique</code> shall perform this initialization via the
5256 expression <code>std::allocator_traits<A2>::construct(a2, p)</code>, where <code>p</code> points to
5257 storage suitable to hold an object of type <code>U</code> and <code>a2</code> of type <code>A2</code> is a
5258 potentially rebound copy of <code>a</code>.</p>
5261 <p>Array elements are initialized in ascending order of their addresses.</p>
5264 <p>When the lifetime of the object managed by the return value ends, or when the
5265 initialization of an array element throws an exception, the initialized
5266 elements should be destroyed in the reverse order of their construction.</p>
5275 <h3 id="allocate_unique_free_functions">Free Functions</h3>
5276 <div class="listingblock">
5277 <div class="content">
5278 <pre class="highlight"><code>template<class T, class A, class... Args>
5279 std::unique_ptr<T, alloc_deleter<T, A>>
5280 allocate_unique(const A& a, Args&&... args);</code></pre>
5283 <div class="ulist none">
5289 <dt class="hdlist1">Constraints</dt>
5291 <p><code>T</code> is not an array.</p>
5293 <dt class="hdlist1">Returns</dt>
5295 <p>A <code>std::unique_ptr</code> to an object of type <code>T</code>, constructed from
5296 <code>args...</code>.</p>
5298 <dt class="hdlist1">Examples</dt>
5303 <p><code>auto p = allocate_unique<int>(a);</code></p>
5306 <p><code>auto p = allocate_unique<std::vector<int>>(a, 16, 1);</code></p>
5310 <div class="listingblock">
5311 <div class="content">
5312 <pre class="highlight"><code>template<class T, class A>
5313 std::unique_ptr<T, alloc_deleter<T, A>>
5314 allocate_unique(const A& a, type_identity_t<T>&& v);</code></pre>
5317 <div class="ulist none">
5323 <dt class="hdlist1">Constraints</dt>
5325 <p><code>T</code> is not an array.</p>
5327 <dt class="hdlist1">Returns</dt>
5329 <p>A <code>std::unique_ptr</code> to an object of type <code>T</code>, constructed from <code>v</code>.</p>
5331 <dt class="hdlist1">Example</dt>
5333 <p><code>auto p = allocate_unique<std::vector<int>>(a, {1, 2});</code></p>
5340 <div class="listingblock">
5341 <div class="content">
5342 <pre class="highlight"><code>template<class T, class A>
5343 std::unique_ptr<T, alloc_deleter<T, A>>
5344 allocate_unique(const A& a, std::size_t n);</code></pre>
5347 <div class="ulist none">
5353 <dt class="hdlist1">Constraints</dt>
5355 <p><code>T</code> is an array of unknown bounds.</p>
5357 <dt class="hdlist1">Returns</dt>
5359 <p>A <code>std::unique_ptr</code> to a sequence of <code>n</code> value-initialized objects of
5360 type <code>remove_extent_t<T></code>.</p>
5362 <dt class="hdlist1">Examples</dt>
5367 <p><code>auto p = allocate_unique<double[]>(a, 1024);</code></p>
5370 <p><code>auto p = allocate_unique<double[][2][2]>(a, 6);</code></p>
5374 <div class="listingblock">
5375 <div class="content">
5376 <pre class="highlight"><code>template<class T, class A>
5377 std::unique_ptr<remove_extent_t<T>[], alloc_deleter<T, A>>
5378 allocate_unique(const A& a);</code></pre>
5381 <div class="ulist none">
5387 <dt class="hdlist1">Constraints</dt>
5389 <p><code>T</code> is an array of known bounds.</p>
5391 <dt class="hdlist1">Returns</dt>
5393 <p>A <code>std::unique_ptr</code> to a sequence of <code>extent_v<T></code> value-initialized
5394 objects of type <code>remove_extent_t<T></code>.</p>
5396 <dt class="hdlist1">Examples</dt>
5401 <p><code>auto p = allocate_unique<double[1024]>(a);</code></p>
5404 <p><code>auto p = allocate_unique<double[6][2][2]>(a);</code></p>
5408 <div class="listingblock">
5409 <div class="content">
5410 <pre class="highlight"><code>template<class T, class A>
5411 std::unique_ptr<T, alloc_deleter<T, A>>
5412 allocate_unique(const A& a, std::size_t n, const remove_extent_t<T>& v);</code></pre>
5415 <div class="ulist none">
5421 <dt class="hdlist1">Constraints</dt>
5423 <p><code>T</code> is an array of unknown bounds.</p>
5425 <dt class="hdlist1">Returns</dt>
5427 <p>A <code>std::unique_ptr</code> to a sequence of <code>n</code> objects of type
5428 <code>remove_extent_t<T></code>, each initialized to <code>v</code>.</p>
5430 <dt class="hdlist1">Examples</dt>
5435 <p><code>auto p = allocate_unique<double[]>(a, 1024, 1.0);</code></p>
5438 <p><code>auto p = allocate_unique<double[][2]>(a, 6, {1.0, 0.0});</code></p>
5441 <p><code>auto p = allocate_unique<std::vector<int>[]>(a, 4, {1, 2});</code></p>
5445 <div class="listingblock">
5446 <div class="content">
5447 <pre class="highlight"><code>template<class T, class A>
5448 std::unique_ptr<remove_extent_t<T>[], alloc_deleter<T, A>>
5449 allocate_unique(const A& a, const remove_extent_t<T>& v);</code></pre>
5452 <div class="ulist none">
5458 <dt class="hdlist1">Constraints</dt>
5460 <p><code>T</code> is an array of known bounds.</p>
5462 <dt class="hdlist1">Returns</dt>
5464 <p>A <code>std::unique_ptr</code> to a sequence of <code>extent_v<T></code> objects of type
5465 <code>remove_extent_t<T></code>, each initialized to <code>v</code>.</p>
5467 <dt class="hdlist1">Examples</dt>
5472 <p><code>auto p = allocate_unique<double[1024]>(a, 1.0);</code></p>
5475 <p><code>auto p = allocate_unique<double[6][2]>(a, {1.0, 0.0});</code></p>
5478 <p><code>auto p = allocate_unique<std::vector<int>[4]>(a, {1, 2});</code></p>
5482 <div class="listingblock">
5483 <div class="content">
5484 <pre class="highlight"><code>template<class T, class A>
5485 std::unique_ptr<T, alloc_noinit_deleter<T, A>>
5486 allocate_unique_noinit(const A& a);</code></pre>
5489 <div class="ulist none">
5495 <dt class="hdlist1">Constraints</dt>
5497 <p><code>T</code> is not an array.</p>
5499 <dt class="hdlist1">Returns</dt>
5501 <p>A <code>std::unique_ptr</code> to a default-initialized object of type <code>T</code>.</p>
5503 <dt class="hdlist1">Example</dt>
5505 <p><code>auto p = allocate_unique_noinit<double>(a);</code></p>
5512 <div class="listingblock">
5513 <div class="content">
5514 <pre class="highlight"><code>template<class T, class A>
5515 std::unique_ptr<T, alloc_noinit_deleter<T, A>>
5516 allocate_unique_noinit(const A& a, std::size_t n);</code></pre>
5519 <div class="ulist none">
5525 <dt class="hdlist1">Constraints</dt>
5527 <p><code>T</code> is an array of unknown bounds.</p>
5529 <dt class="hdlist1">Returns</dt>
5531 <p>A <code>std::unique_ptr</code> to a sequence of <code>n</code> default-initialized objects
5532 of type <code>remove_extent_t<T></code>.</p>
5534 <dt class="hdlist1">Example</dt>
5536 <p><code>auto p = allocate_unique_noinit<double[]>(a, 1024);</code></p>
5543 <div class="listingblock">
5544 <div class="content">
5545 <pre class="highlight"><code>template<class T, class A>
5546 std::unique_ptr<remove_extent_t<T>, alloc_noinit_deleter<T, A>>
5547 allocate_unique_noinit(const A& a);</code></pre>
5550 <div class="ulist none">
5556 <dt class="hdlist1">Constraints</dt>
5558 <p><code>T</code> is an array of known bounds.</p>
5560 <dt class="hdlist1">Returns</dt>
5562 <p>A <code>std::unique_ptr</code> to a sequence of <code>extent_v<T></code>
5563 default-initialized objects of type <code>remove_extent_t<T></code>.</p>
5565 <dt class="hdlist1">Example</dt>
5567 <p><code>auto p = allocate_unique_noinit<double[1024]>(a);</code></p>
5576 <h3 id="allocate_unique_deleter">Deleter</h3>
5577 <div class="paragraph">
5578 <p>Class template <code>alloc_deleter</code> is the deleter used by the <code>allocate_unique</code>
5582 <h4 id="allocate_unique_synopsis_2">Synopsis</h4>
5583 <div class="listingblock">
5584 <div class="content">
5585 <pre class="highlight"><code>template<class T, class A>
5586 class alloc_deleter {
5588 using pointer = <code>unspecified</code>;
5590 explicit alloc_deleter(const A& a) noexcept;
5592 void operator()(pointer p);
5598 <h4 id="allocate_unique_members">Members</h4>
5599 <div class="listingblock">
5600 <div class="content">
5601 <pre class="highlight"><code>using pointer = <code>unspecified</code>;</code></pre>
5604 <div class="ulist none">
5608 <div class="paragraph">
5609 <p>A type that satisfies <em>NullablePointer</em>.</p>
5614 <div class="listingblock">
5615 <div class="content">
5616 <pre class="highlight"><code>explicit alloc_deleter(const A& a) noexcept;</code></pre>
5619 <div class="ulist none">
5625 <dt class="hdlist1">Effects</dt>
5627 <p>Initializes the stored allocator from <code>a</code>.</p>
5634 <div class="listingblock">
5635 <div class="content">
5636 <pre class="highlight"><code>void operator()(pointer p);</code></pre>
5639 <div class="ulist none">
5645 <dt class="hdlist1">Effects</dt>
5647 <p>Destroys the objects and deallocates the storage referenced by <code>p</code>,
5648 using the stored allocator.</p>
5660 <h2 id="intrusive_ptr">intrusive_ptr: Managing Objects with Embedded Counts</h2>
5661 <div class="sectionbody">
5663 <h3 id="intrusive_ptr_description">Description</h3>
5664 <div class="paragraph">
5665 <p>The <code>intrusive_ptr</code> class template stores a pointer to an object with an embedded reference count.
5666 Every new <code>intrusive_ptr</code> instance increments the reference count by using an unqualified call to the
5667 function <code>intrusive_ptr_add_ref</code>, passing it the pointer as an argument. Similarly, when an <code>intrusive_ptr</code>
5668 is destroyed, it calls <code>intrusive_ptr_release</code>; this function is responsible for destroying the object when
5669 its reference count drops to zero. The user is expected to provide suitable definitions of these two functions.
5670 On compilers that support argument-dependent lookup, <code>intrusive_ptr_add_ref</code> and <code>intrusive_ptr_release</code> should
5671 be defined in the namespace that corresponds to their parameter; otherwise, the definitions need to go in namespace
5672 <code>boost</code>. The library provides a helper base class template <code><a href="#intrusive_ref_counter">intrusive_ref_counter</a></code> which
5673 may help adding support for <code>intrusive_ptr</code> to user types.</p>
5675 <div class="paragraph">
5676 <p>The class template is parameterized on <code>T</code>, the type of the object pointed to. <code>intrusive_ptr<T></code> can be implicitly
5677 converted to <code>intrusive_ptr<U></code> whenever <code>T*</code> can be implicitly converted to <code>U*</code>.</p>
5679 <div class="paragraph">
5680 <p>The main reasons to use <code>intrusive_ptr</code> are:</p>
5685 <p>Some existing frameworks or OSes provide objects with embedded reference counts;</p>
5688 <p>The memory footprint of <code>intrusive_ptr</code> is the same as the corresponding raw pointer;</p>
5691 <p><code>intrusive_ptr<T></code> can be constructed from an arbitrary raw pointer of type <code>T*</code>.</p>
5695 <div class="paragraph">
5696 <p>As a general rule, if it isn’t obvious whether <code>intrusive_ptr</code> better fits your needs than <code>shared_ptr</code>, try a <code>shared_ptr</code>-based design first.</p>
5700 <h3 id="intrusive_ptr_synopsis">Synopsis</h3>
5701 <div class="paragraph">
5702 <p><code>intrusive_ptr</code> is defined in <code><boost/smart_ptr/intrusive_ptr.hpp></code>.</p>
5704 <div class="listingblock">
5705 <div class="content">
5706 <pre class="highlight"><code>namespace boost {
5708 template<class T> class intrusive_ptr {
5711 typedef T element_type;
5713 intrusive_ptr() noexcept;
5714 intrusive_ptr(T * p, bool add_ref = true);
5716 intrusive_ptr(intrusive_ptr const & r);
5717 template<class Y> intrusive_ptr(intrusive_ptr<Y> const & r);
5719 intrusive_ptr(intrusive_ptr && r);
5720 template<class Y> intrusive_ptr(intrusive_ptr<Y> && r);
5724 intrusive_ptr & operator=(intrusive_ptr const & r);
5725 template<class Y> intrusive_ptr & operator=(intrusive_ptr<Y> const & r);
5726 intrusive_ptr & operator=(T * r);
5728 intrusive_ptr & operator=(intrusive_ptr && r);
5729 template<class Y> intrusive_ptr & operator=(intrusive_ptr<Y> && r);
5733 void reset(T * r, bool add_ref);
5735 T & operator*() const noexcept;
5736 T * operator->() const noexcept;
5737 T * get() const noexcept;
5738 T * detach() noexcept;
5740 explicit operator bool () const noexcept;
5742 void swap(intrusive_ptr & b) noexcept;
5745 template<class T, class U>
5746 bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b) noexcept;
5748 template<class T, class U>
5749 bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b) noexcept;
5751 template<class T, class U>
5752 bool operator==(intrusive_ptr<T> const & a, U * b) noexcept;
5754 template<class T, class U>
5755 bool operator!=(intrusive_ptr<T> const & a, U * b) noexcept;
5757 template<class T, class U>
5758 bool operator==(T * a, intrusive_ptr<U> const & b) noexcept;
5760 template<class T, class U>
5761 bool operator!=(T * a, intrusive_ptr<U> const & b) noexcept;
5763 template<class T>
5764 bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b) noexcept;
5766 template<class T> void swap(intrusive_ptr<T> & a, intrusive_ptr<T> & b) noexcept;
5768 template<class T> T * get_pointer(intrusive_ptr<T> const & p) noexcept;
5770 template<class T, class U>
5771 intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & r) noexcept;
5773 template<class T, class U>
5774 intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & r) noexcept;
5776 template<class T, class U>
5777 intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & r) noexcept;
5779 template<class E, class T, class Y>
5780 std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os,
5781 intrusive_ptr<Y> const & p);
5787 <h3 id="intrusive_ptr_members">Members</h3>
5789 <h4 id="intrusive_ptr_element_type">element_type</h4>
5790 <div class="listingblock">
5791 <div class="content">
5792 <pre class="highlight"><code>typedef T element_type;</code></pre>
5795 <div class="paragraph">
5796 <p>Provides the type of the template parameter T.</p>
5800 <h4 id="intrusive_ptr_constructors">constructors</h4>
5801 <div class="listingblock">
5802 <div class="content">
5803 <pre class="highlight"><code>intrusive_ptr() noexcept;</code></pre>
5806 <div class="ulist none">
5812 <dt class="hdlist1">Postconditions</dt>
5814 <p><code>get() == 0</code>.</p>
5821 <div class="listingblock">
5822 <div class="content">
5823 <pre class="highlight"><code>intrusive_ptr(T * p, bool add_ref = true);</code></pre>
5826 <div class="ulist none">
5832 <dt class="hdlist1">Effects</dt>
5834 <p><code>if(p != 0 && add_ref) intrusive_ptr_add_ref(p);</code>.</p>
5836 <dt class="hdlist1">Postconditions</dt>
5838 <p><code>get() == p</code>.</p>
5845 <div class="listingblock">
5846 <div class="content">
5847 <pre class="highlight"><code>intrusive_ptr(intrusive_ptr const & r);</code></pre>
5850 <div class="listingblock">
5851 <div class="content">
5852 <pre class="highlight"><code>template<class Y> intrusive_ptr(intrusive_ptr<Y> const & r);</code></pre>
5855 <div class="ulist none">
5861 <dt class="hdlist1">Effects</dt>
5863 <p><code>T * p = r.get(); if(p != 0) intrusive_ptr_add_ref(p);</code>.</p>
5865 <dt class="hdlist1">Postconditions</dt>
5867 <p><code>get() == r.get()</code>.</p>
5874 <div class="listingblock">
5875 <div class="content">
5876 <pre class="highlight"><code>intrusive_ptr(intrusive_ptr && r);</code></pre>
5879 <div class="listingblock">
5880 <div class="content">
5881 <pre class="highlight"><code>template<class Y> intrusive_ptr(intrusive_ptr<Y> && r);</code></pre>
5884 <div class="ulist none">
5890 <dt class="hdlist1">Postconditions</dt>
5892 <p><code>get()</code> equals the old value of <code>r.get()</code>. <code>r.get() == 0</code>.</p>
5901 <h4 id="intrusive_ptr_destructor">destructor</h4>
5902 <div class="listingblock">
5903 <div class="content">
5904 <pre class="highlight"><code>~intrusive_ptr();</code></pre>
5907 <div class="ulist none">
5913 <dt class="hdlist1">Effects</dt>
5915 <p><code>if(get() != 0) intrusive_ptr_release(get());</code>.</p>
5924 <h4 id="intrusive_ptr_assignment">assignment</h4>
5925 <div class="listingblock">
5926 <div class="content">
5927 <pre class="highlight"><code>intrusive_ptr & operator=(intrusive_ptr const & r);</code></pre>
5930 <div class="listingblock">
5931 <div class="content">
5932 <pre class="highlight"><code>template<class Y> intrusive_ptr & operator=(intrusive_ptr<Y> const & r);</code></pre>
5935 <div class="listingblock">
5936 <div class="content">
5937 <pre class="highlight"><code>intrusive_ptr & operator=(T * r);</code></pre>
5940 <div class="ulist none">
5946 <dt class="hdlist1">Effects</dt>
5948 <p>Equivalent to <code>intrusive_ptr(r).swap(*this)</code>.</p>
5950 <dt class="hdlist1">Returns</dt>
5952 <p><code>*this</code>.</p>
5959 <div class="listingblock">
5960 <div class="content">
5961 <pre class="highlight"><code>intrusive_ptr & operator=(intrusive_ptr && r);</code></pre>
5964 <div class="listingblock">
5965 <div class="content">
5966 <pre class="highlight"><code>template<class Y> intrusive_ptr & operator=(intrusive_ptr<Y> && r);</code></pre>
5969 <div class="ulist none">
5975 <dt class="hdlist1">Effects</dt>
5977 <p>Equivalent to <code>intrusive_ptr(std::move(r)).swap(*this)</code>.</p>
5979 <dt class="hdlist1">Returns</dt>
5981 <p><code>*this</code>.</p>
5990 <h4 id="intrusive_ptr_reset">reset</h4>
5991 <div class="listingblock">
5992 <div class="content">
5993 <pre class="highlight"><code>void reset();</code></pre>
5996 <div class="ulist none">
6002 <dt class="hdlist1">Effects</dt>
6004 <p>Equivalent to <code>intrusive_ptr().swap(*this)</code>.</p>
6011 <div class="listingblock">
6012 <div class="content">
6013 <pre class="highlight"><code>void reset(T * r);</code></pre>
6016 <div class="ulist none">
6022 <dt class="hdlist1">Effects</dt>
6024 <p>Equivalent to <code>intrusive_ptr(r).swap(*this)</code>.</p>
6031 <div class="listingblock">
6032 <div class="content">
6033 <pre class="highlight"><code>void reset(T * r, bool add_ref);</code></pre>
6036 <div class="ulist none">
6042 <dt class="hdlist1">Effects</dt>
6044 <p>Equivalent to <code>intrusive_ptr(r, add_ref).swap(*this)</code>.</p>
6053 <h4 id="intrusive_ptr_indirection">indirection</h4>
6054 <div class="listingblock">
6055 <div class="content">
6056 <pre class="highlight"><code>T & operator*() const noexcept;</code></pre>
6059 <div class="ulist none">
6065 <dt class="hdlist1">Requirements</dt>
6067 <p><code>get() != 0</code>.</p>
6069 <dt class="hdlist1">Returns</dt>
6071 <p><code>*get()</code>.</p>
6078 <div class="listingblock">
6079 <div class="content">
6080 <pre class="highlight"><code>T * operator->() const noexcept;</code></pre>
6083 <div class="ulist none">
6089 <dt class="hdlist1">Requirements</dt>
6091 <p><code>get() != 0</code>.</p>
6093 <dt class="hdlist1">Returns</dt>
6095 <p><code>get()</code>.</p>
6104 <h4 id="intrusive_ptr_get">get</h4>
6105 <div class="listingblock">
6106 <div class="content">
6107 <pre class="highlight"><code>T * get() const noexcept;</code></pre>
6110 <div class="ulist none">
6116 <dt class="hdlist1">Returns</dt>
6118 <p>the stored pointer.</p>
6127 <h4 id="intrusive_ptr_detach">detach</h4>
6128 <div class="listingblock">
6129 <div class="content">
6130 <pre class="highlight"><code>T * detach() noexcept;</code></pre>
6133 <div class="ulist none">
6139 <dt class="hdlist1">Returns</dt>
6141 <p>the stored pointer.</p>
6143 <dt class="hdlist1">Postconditions</dt>
6145 <p><code>get() == 0</code>.</p>
6152 <div class="admonitionblock note">
6156 <div class="title">Note</div>
6158 <td class="content">
6159 The returned pointer has an elevated reference count. This allows conversion of an <code>intrusive_ptr</code>
6160 back to a raw pointer, without the performance overhead of acquiring and dropping an extra reference.
6161 It can be viewed as the complement of the non-reference-incrementing constructor.
6166 <div class="admonitionblock caution">
6170 <div class="title">Caution</div>
6172 <td class="content">
6173 Using <code>detach</code> escapes the safety of automatic reference counting provided by <code>intrusive_ptr</code>.
6174 It should by used only where strictly necessary (such as when interfacing to an existing API), and when
6175 the implications are thoroughly understood.
6182 <h4 id="intrusive_ptr_conversions">conversions</h4>
6183 <div class="listingblock">
6184 <div class="content">
6185 <pre class="highlight"><code>explicit operator bool () const noexcept;</code></pre>
6188 <div class="ulist none">
6194 <dt class="hdlist1">Returns</dt>
6196 <p><code>get() != 0</code>.</p>
6203 <div class="admonitionblock note">
6207 <div class="title">Note</div>
6209 <td class="content">
6210 This conversion operator allows <code>intrusive_ptr</code> objects to be used in boolean contexts,
6211 like <code>if (p && p->valid()) {}</code>.
6216 <div class="admonitionblock note">
6220 <div class="title">Note</div>
6222 <td class="content">
6223 On C++03 compilers, the return value is of an unspecified type.
6230 <h4 id="intrusive_ptr_swap">swap</h4>
6231 <div class="listingblock">
6232 <div class="content">
6233 <pre class="highlight"><code>void swap(intrusive_ptr & b) noexcept;</code></pre>
6236 <div class="ulist none">
6242 <dt class="hdlist1">Effects</dt>
6244 <p>Exchanges the contents of the two smart pointers.</p>
6254 <h3 id="intrusive_ptr_free_functions">Free Functions</h3>
6256 <h4 id="intrusive_ptr_comparison">comparison</h4>
6257 <div class="listingblock">
6258 <div class="content">
6259 <pre class="highlight"><code>template<class T, class U>
6260 bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b) noexcept;</code></pre>
6263 <div class="ulist none">
6269 <dt class="hdlist1">Returns</dt>
6271 <p><code>a.get() == b.get()</code>.</p>
6278 <div class="listingblock">
6279 <div class="content">
6280 <pre class="highlight"><code>template<class T, class U>
6281 bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b) noexcept;</code></pre>
6284 <div class="ulist none">
6290 <dt class="hdlist1">Returns</dt>
6292 <p><code>a.get() != b.get()</code>.</p>
6299 <div class="listingblock">
6300 <div class="content">
6301 <pre class="highlight"><code>template<class T, class U>
6302 bool operator==(intrusive_ptr<T> const & a, U * b) noexcept;</code></pre>
6305 <div class="ulist none">
6311 <dt class="hdlist1">Returns</dt>
6313 <p><code>a.get() == b</code>.</p>
6320 <div class="listingblock">
6321 <div class="content">
6322 <pre class="highlight"><code>template<class T, class U>
6323 bool operator!=(intrusive_ptr<T> const & a, U * b) noexcept;</code></pre>
6326 <div class="ulist none">
6332 <dt class="hdlist1">Returns</dt>
6334 <p><code>a.get() != b</code>.</p>
6341 <div class="listingblock">
6342 <div class="content">
6343 <pre class="highlight"><code>template<class T, class U>
6344 bool operator==(T * a, intrusive_ptr<U> const & b) noexcept;</code></pre>
6347 <div class="ulist none">
6353 <dt class="hdlist1">Returns</dt>
6355 <p><code>a == b.get()</code>.</p>
6362 <div class="listingblock">
6363 <div class="content">
6364 <pre class="highlight"><code>template<class T, class U>
6365 bool operator!=(T * a, intrusive_ptr<U> const & b) noexcept;</code></pre>
6368 <div class="ulist none">
6374 <dt class="hdlist1">Returns</dt>
6376 <p><code>a != b.get()</code>.</p>
6383 <div class="listingblock">
6384 <div class="content">
6385 <pre class="highlight"><code>template<class T>
6386 bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b) noexcept;</code></pre>
6389 <div class="ulist none">
6395 <dt class="hdlist1">Returns</dt>
6397 <p><code>std::less<T *>()(a.get(), b.get())</code>.</p>
6404 <div class="admonitionblock note">
6408 <div class="title">Note</div>
6410 <td class="content">
6411 Allows <code>intrusive_ptr</code> objects to be used as keys in associative containers.
6418 <h4 id="intrusive_ptr_swap_2">swap</h4>
6419 <div class="listingblock">
6420 <div class="content">
6421 <pre class="highlight"><code>template<class T> void swap(intrusive_ptr<T> & a, intrusive_ptr<T> & b) noexcept;</code></pre>
6424 <div class="ulist none">
6430 <dt class="hdlist1">Effects</dt>
6432 <p>Equivalent to <code>a.swap(b)</code>.</p>
6441 <h4 id="intrusive_ptr_get_pointer">get_pointer</h4>
6442 <div class="listingblock">
6443 <div class="content">
6444 <pre class="highlight"><code>template<class T> T * get_pointer(intrusive_ptr<T> const & p) noexcept;</code></pre>
6447 <div class="ulist none">
6453 <dt class="hdlist1">Returns</dt>
6455 <p><code>p.get()</code>.</p>
6462 <div class="admonitionblock note">
6466 <div class="title">Note</div>
6468 <td class="content">
6469 Provided as an aid to generic programming. Used by <code>mem_fn</code>.
6476 <h4 id="intrusive_ptr_static_pointer_cast">static_pointer_cast</h4>
6477 <div class="listingblock">
6478 <div class="content">
6479 <pre class="highlight"><code>template<class T, class U>
6480 intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & r) noexcept;</code></pre>
6483 <div class="ulist none">
6489 <dt class="hdlist1">Returns</dt>
6491 <p><code>intrusive_ptr<T>(static_cast<T*>(r.get()))</code>.</p>
6500 <h4 id="intrusive_ptr_const_pointer_cast">const_pointer_cast</h4>
6501 <div class="listingblock">
6502 <div class="content">
6503 <pre class="highlight"><code>template<class T, class U>
6504 intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & r) noexcept;</code></pre>
6507 <div class="ulist none">
6513 <dt class="hdlist1">Returns</dt>
6515 <p><code>intrusive_ptr<T>(const_cast<T*>(r.get()))</code>.</p>
6524 <h4 id="intrusive_ptr_dynamic_pointer_cast">dynamic_pointer_cast</h4>
6525 <div class="listingblock">
6526 <div class="content">
6527 <pre class="highlight"><code>template<class T, class U>
6528 intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & r) noexcept;</code></pre>
6531 <div class="ulist none">
6537 <dt class="hdlist1">Returns</dt>
6539 <p><code>intrusive_ptr<T>(dynamic_cast<T*>(r.get()))</code>.</p>
6548 <h4 id="intrusive_ptr_operator">operator<<</h4>
6549 <div class="listingblock">
6550 <div class="content">
6551 <pre class="highlight"><code>template<class E, class T, class Y>
6552 std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os,
6553 intrusive_ptr<Y> const & p);</code></pre>
6556 <div class="ulist none">
6562 <dt class="hdlist1">Effects</dt>
6564 <p><code>os << p.get();</code>.</p>
6566 <dt class="hdlist1">Returns</dt>
6568 <p><code>os</code>.</p>
6580 <h2 id="intrusive_ref_counter">intrusive_ref_counter</h2>
6581 <div class="sectionbody">
6583 <h3 id="intrusive_ref_counter_description">Description</h3>
6584 <div class="paragraph">
6585 <p>The <code>intrusive_ref_counter</code> class template implements a reference counter for
6586 a derived user’s class that is intended to be used with <code>intrusive_ptr</code>. The
6587 base class has associated <code>intrusive_ptr_add_ref</code> and <code>intrusive_ptr_release</code>
6588 functions which modify the reference counter as needed and destroy the user’s
6589 object when the counter drops to zero.</p>
6591 <div class="paragraph">
6592 <p>The class template is parameterized on <code>Derived</code> and <code>CounterPolicy</code>
6593 parameters. The first parameter is the user’s class that derives from
6594 <code>intrusive_ref_counter</code>. This type is needed in order to destroy the object
6595 correctly when there are no references to it left.</p>
6597 <div class="paragraph">
6598 <p>The second parameter is a policy that defines the nature of the reference
6599 counter. The library provides two such policies: <code>thread_unsafe_counter</code> and
6600 <code>thread_safe_counter</code>. The former instructs the <code>intrusive_ref_counter</code> base
6601 class to use a counter only suitable for a single-threaded use. Pointers to a
6602 single object that uses this kind of reference counter must not be used in
6603 different threads. The latter policy makes the reference counter thread-safe,
6604 unless the target platform doesn’t support threading. Since in modern systems
6605 support for threading is common, the default counter policy is
6606 <code>thread_safe_counter</code>.</p>
6610 <h3 id="intrusive_ref_counter_synopsis">Synopsis</h3>
6611 <div class="paragraph">
6612 <p><code>intrusive_ref_counter</code> is defined in
6613 <code><boost/smart_ptr/intrusive_ref_counter.hpp></code>.</p>
6615 <div class="listingblock">
6616 <div class="content">
6617 <pre class="highlight"><code>namespace boost {
6618 struct thread_unsafe_counter;
6619 struct thread_safe_counter;
6621 template<class Derived, class CounterPolicy = thread_safe_counter>
6622 class intrusive_ref_counter {
6624 intrusive_ref_counter() noexcept;
6625 intrusive_ref_counter(const intrusive_ref_counter& v) noexcept;
6627 intrusive_ref_counter& operator=(const intrusive_ref_counter& v) noexcept;
6629 unsigned int use_count() const noexcept;
6632 ~intrusive_ref_counter() = default;
6635 template<class Derived, class CounterPolicy>
6636 void intrusive_ptr_add_ref(
6637 const intrusive_ref_counter<Derived, CounterPolicy>* p) noexcept;
6639 template<class Derived, class CounterPolicy>
6640 void intrusive_ptr_release(
6641 const intrusive_ref_counter<Derived, CounterPolicy>* p) noexcept;
6647 <h3 id="intrusive_ref_counter_members">Members</h3>
6649 <h4 id="intrusive_ref_counter_constructors">Constructors</h4>
6650 <div class="listingblock">
6651 <div class="content">
6652 <pre class="highlight"><code>intrusive_ref_counter() noexcept;</code></pre>
6655 <div class="listingblock">
6656 <div class="content">
6657 <pre class="highlight"><code>intrusive_ref_counter(const intrusive_ref_counter&) noexcept;</code></pre>
6660 <div class="ulist none">
6666 <dt class="hdlist1">Postconditions</dt>
6668 <p><code>use_count() == 0</code>.</p>
6675 <div class="admonitionblock note">
6679 <div class="title">Note</div>
6681 <td class="content">
6682 The pointer to the constructed object is expected to be passed to
6683 <code>intrusive_ptr</code> constructor, assignment operator or <code>reset</code> method, which
6684 would increment the reference counter.
6691 <h4 id="intrusive_ref_counter_destructor">Destructor</h4>
6692 <div class="listingblock">
6693 <div class="content">
6694 <pre class="highlight"><code>~intrusive_ref_counter();</code></pre>
6697 <div class="ulist none">
6703 <dt class="hdlist1">Effects</dt>
6705 <p>Destroys the counter object.</p>
6712 <div class="admonitionblock note">
6716 <div class="title">Note</div>
6718 <td class="content">
6719 The destructor is protected so that the object can only be destroyed
6720 through the <code>Derived</code> class.
6727 <h4 id="intrusive_ref_counter_assignment">Assignment</h4>
6728 <div class="listingblock">
6729 <div class="content">
6730 <pre class="highlight"><code>intrusive_ref_counter& operator=(const intrusive_ref_counter& v) noexcept;</code></pre>
6733 <div class="ulist none">
6739 <dt class="hdlist1">Effects</dt>
6741 <p>Does nothing, reference counter is not modified.</p>
6750 <h4 id="intrusive_ref_counter_use_count">use_count</h4>
6751 <div class="listingblock">
6752 <div class="content">
6753 <pre class="highlight"><code>unsigned int use_count() const noexcept;</code></pre>
6756 <div class="ulist none">
6762 <dt class="hdlist1">Returns</dt>
6764 <p>The current value of the reference counter.</p>
6771 <div class="admonitionblock note">
6775 <div class="title">Note</div>
6777 <td class="content">
6778 The returned value may not be actual in multi-threaded applications.
6786 <h3 id="intrusive_ref_counter_free_functions">Free Functions</h3>
6788 <h4 id="intrusive_ref_counter_intrusive_ptr_add_ref">intrusive_ptr_add_ref</h4>
6789 <div class="listingblock">
6790 <div class="content">
6791 <pre class="highlight"><code>template<class Derived, class CounterPolicy>
6792 void intrusive_ptr_add_ref(
6793 const intrusive_ref_counter<Derived, CounterPolicy>* p) noexcept;</code></pre>
6796 <div class="ulist none">
6802 <dt class="hdlist1">Effects</dt>
6804 <p>Increments the reference counter.</p>
6813 <h4 id="intrusive_ref_counter_intrusive_ptr_release">intrusive_ptr_release</h4>
6814 <div class="listingblock">
6815 <div class="content">
6816 <pre class="highlight"><code>template<class Derived, class CounterPolicy>
6817 void intrusive_ptr_release(
6818 const intrusive_ref_counter<Derived, CounterPolicy>* p) noexcept;</code></pre>
6821 <div class="ulist none">
6827 <dt class="hdlist1">Effects</dt>
6829 <p>Decrements the reference counter. If the reference counter reaches
6830 0, calls <code>delete static_cast<const Derived*>(p)</code>.</p>
6842 <h2 id="local_shared_ptr">local_shared_ptr: Shared Ownership within a Single Thread</h2>
6843 <div class="sectionbody">
6845 <h3 id="local_shared_ptr_description">Description</h3>
6846 <div class="paragraph">
6847 <p><code>local_shared_ptr</code> is nearly identical to <code>shared_ptr</code>, with the only difference of note being that its reference count is
6848 updated with non-atomic operations. As such, a <code>local_shared_ptr</code> and all its copies must reside in (be local to) a single
6849 thread (hence the name.)</p>
6851 <div class="paragraph">
6852 <p><code>local_shared_ptr</code> can be converted to <code>shared_ptr</code> and vice versa. Creating a <code>local_shared_ptr</code> from a <code>shared_ptr</code> creates
6853 a new local reference count; this means that two <code>local_shared_ptr</code> instances, both created from the same <code>shared_ptr</code>, refer
6854 to the same object but don’t share the same count, and as such, can safely be used by two different threads.</p>
6856 <div class="listingblock">
6857 <div class="title">Code Example 9. Two local_shared_ptr instances created from a shared_ptr</div>
6858 <div class="content">
6859 <pre class="highlight"><code>shared_ptr<X> p1( new X );
6861 local_shared_ptr<X> p2( p1 ); // p2.local_use_count() == 1
6862 local_shared_ptr<X> p3( p1 ); // p3.local_use_count() also 1</code></pre>
6865 <div class="paragraph">
6866 <p>Creating the second <code>local_shared_ptr</code> from the first one, however, does lead to the two sharing the same count:</p>
6868 <div class="listingblock">
6869 <div class="title">Code Example 10. A local_shared_ptr created from another local_shared_ptr</div>
6870 <div class="content">
6871 <pre class="highlight"><code>shared_ptr<X> p1( new X );
6873 local_shared_ptr<X> p2( p1 ); // p2.local_use_count() == 1
6874 local_shared_ptr<X> p3( p2 ); // p3.local_use_count() == 2</code></pre>
6877 <div class="paragraph">
6878 <p>Two <code>shared_ptr</code> instances created from the same <code>local_shared_ptr</code> do share ownership:</p>
6880 <div class="listingblock">
6881 <div class="title">Code Example 11. Two shared_ptr instances created from a local_shared_ptr</div>
6882 <div class="content">
6883 <pre class="highlight"><code>local_shared_ptr<X> p1( new X );
6885 shared_ptr<X> p2( p1 ); // p2.use_count() == 2
6886 shared_ptr<X> p3( p1 ); // p3.use_count() == 3</code></pre>
6889 <div class="paragraph">
6890 <p>Here <code>p2.use_count()</code> is 2, because <code>p1</code> holds a reference, too.</p>
6892 <div class="paragraph">
6893 <p>One can think of <code>local_shared_ptr<T></code> as <code>shared_ptr<shared_ptr<T>></code>, with the outer <code>shared_ptr</code> using non-atomic operations for
6894 its count. Converting from <code>local_shared_ptr</code> to <code>shared_ptr</code> gives you a copy of the inner <code>shared_ptr</code>; converting from <code>shared_ptr</code>
6895 wraps it into an outer <code>shared_ptr</code> with a non-atomic use count (conceptually speaking) and returns the result.</p>
6899 <h3 id="local_shared_ptr_synopsis">Synopsis</h3>
6900 <div class="paragraph">
6901 <p><code>local_shared_ptr</code> is defined in <code><boost/smart_ptr/local_shared_ptr.hpp></code>.</p>
6903 <div class="listingblock">
6904 <div class="content">
6905 <pre class="highlight"><code>namespace boost {
6907 template<class T> class local_shared_ptr {
6910 typedef /*see below*/ element_type;
6914 constexpr local_shared_ptr() noexcept;
6915 constexpr local_shared_ptr(std::nullptr_t) noexcept;
6917 template<class Y> explicit local_shared_ptr(Y * p);
6919 template<class Y, class D> local_shared_ptr(Y * p, D d);
6920 template<class D> local_shared_ptr(std::nullptr_t p, D d);
6922 template<class Y, class D, class A> local_shared_ptr(Y * p, D d, A a);
6923 template<class D, class A> local_shared_ptr(std::nullptr_t p, D d, A a);
6925 local_shared_ptr(local_shared_ptr const & r) noexcept;
6926 template<class Y> local_shared_ptr(local_shared_ptr<Y> const & r) noexcept;
6928 local_shared_ptr(local_shared_ptr && r) noexcept;
6929 template<class Y> local_shared_ptr(local_shared_ptr<Y> && r) noexcept;
6931 template<class Y> local_shared_ptr( shared_ptr<Y> const & r );
6932 template<class Y> local_shared_ptr( shared_ptr<Y> && r );
6934 template<class Y> local_shared_ptr(local_shared_ptr<Y> const & r, element_type * p) noexcept;
6935 template<class Y> local_shared_ptr(local_shared_ptr<Y> && r, element_type * p) noexcept;
6937 template<class Y, class D> local_shared_ptr(std::unique_ptr<Y, D> && r);
6941 ~local_shared_ptr() noexcept;
6945 local_shared_ptr & operator=(local_shared_ptr const & r) noexcept;
6946 template<class Y> local_shared_ptr & operator=(local_shared_ptr<Y> const & r) noexcept;
6948 local_shared_ptr & operator=(local_shared_ptr const && r) noexcept;
6949 template<class Y> local_shared_ptr & operator=(local_shared_ptr<Y> const && r) noexcept;
6951 template<class Y, class D> local_shared_ptr & operator=(std::unique_ptr<Y, D> && r);
6953 local_shared_ptr & operator=(std::nullptr_t) noexcept;
6957 void reset() noexcept;
6959 template<class Y> void reset(Y * p);
6960 template<class Y, class D> void reset(Y * p, D d);
6961 template<class Y, class D, class A> void reset(Y * p, D d, A a);
6963 template<class Y> void reset(local_shared_ptr<Y> const & r, element_type * p) noexcept;
6964 template<class Y> void reset(local_shared_ptr<Y> && r, element_type * p) noexcept;
6968 T & operator*() const noexcept; // only valid when T is not an array type
6969 T * operator->() const noexcept; // only valid when T is not an array type
6971 // only valid when T is an array type
6972 element_type & operator[](std::ptrdiff_t i) const noexcept;
6974 element_type * get() const noexcept;
6976 long local_use_count() const noexcept;
6980 explicit operator bool() const noexcept;
6982 template<class Y> operator shared_ptr<Y>() const noexcept;
6983 template<class Y> operator weak_ptr<Y>() const noexcept;
6987 void swap(local_shared_ptr & b) noexcept;
6991 template<class Y> bool owner_before(local_shared_ptr<Y> const & rhs) const noexcept;
6996 template<class T, class U>
6997 bool operator==(local_shared_ptr<T> const & a, local_shared_ptr<U> const & b) noexcept;
6998 template<class T, class U>
6999 bool operator==(local_shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept;
7000 template<class T, class U>
7001 bool operator==(shared_ptr<T> const & a, local_shared_ptr<U> const & b) noexcept;
7003 template<class T, class U>
7004 bool operator!=(local_shared_ptr<T> const & a, local_shared_ptr<U> const & b) noexcept;
7005 template<class T, class U>
7006 bool operator!=(local_shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept;
7007 template<class T, class U>
7008 bool operator!=(shared_ptr<T> const & a, local_shared_ptr<U> const & b) noexcept;
7010 template<class T> bool operator==(local_shared_ptr<T> const & p, std::nullptr_t) noexcept;
7011 template<class T> bool operator==(std::nullptr_t, local_shared_ptr<T> const & p) noexcept;
7013 template<class T> bool operator!=(local_shared_ptr<T> const & p, std::nullptr_t) noexcept;
7014 template<class T> bool operator!=(std::nullptr_t, local_shared_ptr<T> const & p) noexcept;
7016 template<class T, class U>
7017 bool operator<(local_shared_ptr<T> const & a, local_shared_ptr<U> const & b) noexcept;
7021 template<class T> void swap(local_shared_ptr<T> & a, local_shared_ptr<T> & b) noexcept;
7025 template<class T>
7026 typename local_shared_ptr<T>::element_type *
7027 get_pointer(local_shared_ptr<T> const & p) noexcept;
7031 template<class T, class U>
7032 local_shared_ptr<T> static_pointer_cast(local_shared_ptr<U> const & r) noexcept;
7034 template<class T, class U>
7035 local_shared_ptr<T> const_pointer_cast(local_shared_ptr<U> const & r) noexcept;
7037 template<class T, class U>
7038 local_shared_ptr<T> dynamic_pointer_cast(local_shared_ptr<U> const & r) noexcept;
7040 template<class T, class U>
7041 local_shared_ptr<T> reinterpret_pointer_cast(local_shared_ptr<U> const & r) noexcept;
7045 template<class E, class T, class Y>
7046 std::basic_ostream<E, T> &
7047 operator<< (std::basic_ostream<E, T> & os, local_shared_ptr<Y> const & p);
7051 template<class D, class T> D * get_deleter(local_shared_ptr<T> const & p) noexcept;
7057 <h3 id="local_shared_ptr_members">Members</h3>
7059 <h4 id="local_shared_ptr_element_type">element_type</h4>
7060 <div class="listingblock">
7061 <div class="content">
7062 <pre class="highlight"><code>typedef ... element_type;</code></pre>
7065 <div class="paragraph">
7066 <p><code>element_type</code> is <code>T</code> when <code>T</code> is not an array type, and <code>U</code> when <code>T</code> is <code>U[]</code> or <code>U[N]</code>.</p>
7070 <h4 id="local_shared_ptr_default_constructor">default constructor</h4>
7071 <div class="listingblock">
7072 <div class="content">
7073 <pre class="highlight"><code>constexpr local_shared_ptr() noexcept;</code></pre>
7076 <div class="listingblock">
7077 <div class="content">
7078 <pre class="highlight"><code>constexpr local_shared_ptr(std::nullptr_t) noexcept;</code></pre>
7081 <div class="ulist none">
7087 <dt class="hdlist1">Effects</dt>
7089 <p>Constructs an empty <code>local_shared_ptr</code>.</p>
7091 <dt class="hdlist1">Postconditions</dt>
7093 <p><code>local_use_count() == 0 && get() == 0</code>.</p>
7102 <h4 id="local_shared_ptr_pointer_constructor">pointer constructor</h4>
7103 <div class="listingblock">
7104 <div class="content">
7105 <pre class="highlight"><code>template<class Y> explicit local_shared_ptr(Y * p);</code></pre>
7108 <div class="ulist none">
7114 <dt class="hdlist1">Effects</dt>
7116 <p>Constructs a <code>local_shared_ptr</code> that owns <code>shared_ptr<T>( p )</code>.</p>
7118 <dt class="hdlist1">Postconditions</dt>
7120 <p><code>local_use_count() == 1 && get() == p</code>.</p>
7122 <dt class="hdlist1">Throws</dt>
7124 <p><code>std::bad_alloc</code>, or an implementation-defined exception when a resource other than memory could not be obtained.</p>
7133 <h4 id="local_shared_ptr_constructors_taking_a_deleter">constructors taking a deleter</h4>
7134 <div class="listingblock">
7135 <div class="content">
7136 <pre class="highlight"><code>template<class Y, class D> local_shared_ptr(Y * p, D d);</code></pre>
7139 <div class="listingblock">
7140 <div class="content">
7141 <pre class="highlight"><code>template<class D> local_shared_ptr(std::nullptr_t p, D d);</code></pre>
7144 <div class="ulist none">
7150 <dt class="hdlist1">Effects</dt>
7152 <p>Constructs a <code>local_shared_ptr</code> that owns <code>shared_ptr<T>( p, d )</code>.</p>
7154 <dt class="hdlist1">Postconditions</dt>
7156 <p><code>local_use_count() == 1 && get() == p</code>.</p>
7158 <dt class="hdlist1">Throws</dt>
7160 <p><code>std::bad_alloc</code>, or an implementation-defined exception when a resource other than memory could not be obtained.</p>
7167 <div class="listingblock">
7168 <div class="content">
7169 <pre class="highlight"><code>template<class Y, class D, class A> local_shared_ptr(Y * p, D d, A a);</code></pre>
7172 <div class="listingblock">
7173 <div class="content">
7174 <pre class="highlight"><code>template<class D, class A> local_shared_ptr(std::nullptr_t p, D d, A a);</code></pre>
7177 <div class="ulist none">
7183 <dt class="hdlist1">Effects</dt>
7185 <p>Constructs a <code>local_shared_ptr</code> that owns <code>shared_ptr<T>( p, d, a )</code>.</p>
7187 <dt class="hdlist1">Postconditions</dt>
7189 <p><code>local_use_count() == 1 && get() == p</code>.</p>
7191 <dt class="hdlist1">Throws</dt>
7193 <p><code>std::bad_alloc</code>, or an implementation-defined exception when a resource other than memory could not be obtained.</p>
7202 <h4 id="local_shared_ptr_copy_and_converting_constructors">copy and converting constructors</h4>
7203 <div class="listingblock">
7204 <div class="content">
7205 <pre class="highlight"><code>local_shared_ptr(local_shared_ptr const & r) noexcept;</code></pre>
7208 <div class="listingblock">
7209 <div class="content">
7210 <pre class="highlight"><code>template<class Y> local_shared_ptr(local_shared_ptr<Y> const & r) noexcept;</code></pre>
7213 <div class="ulist none">
7219 <dt class="hdlist1">Requires</dt>
7221 <p><code>Y*</code> should be convertible to <code>T*</code>.</p>
7223 <dt class="hdlist1">Effects</dt>
7225 <p>If <code>r</code> is empty, constructs an empty <code>local_shared_ptr</code>; otherwise, constructs a <code>local_shared_ptr</code> that shares ownership with <code>r</code>.</p>
7227 <dt class="hdlist1">Postconditions</dt>
7229 <p><code>get() == r.get() && local_use_count() == r.local_use_count()</code>.</p>
7238 <h4 id="local_shared_ptr_move_constructors">move constructors</h4>
7239 <div class="listingblock">
7240 <div class="content">
7241 <pre class="highlight"><code>local_shared_ptr(local_shared_ptr && r) noexcept;</code></pre>
7244 <div class="listingblock">
7245 <div class="content">
7246 <pre class="highlight"><code>template<class Y> local_shared_ptr(local_shared_ptr<Y> && r) noexcept;</code></pre>
7249 <div class="ulist none">
7255 <dt class="hdlist1">Requires</dt>
7257 <p><code>Y*</code> should be convertible to <code>T*</code>.</p>
7259 <dt class="hdlist1">Effects</dt>
7261 <p>Move-constructs a <code>local_shared_ptr</code> from <code>r</code>.</p>
7263 <dt class="hdlist1">Postconditions</dt>
7265 <p><code>*this</code> contains the old value of <code>r</code>. <code>r</code> is empty and <code>r.get() == 0</code>.</p>
7274 <h4 id="local_shared_ptr_shared_ptr_constructor">shared_ptr constructor</h4>
7275 <div class="listingblock">
7276 <div class="content">
7277 <pre class="highlight"><code>template<class Y> local_shared_ptr( shared_ptr<Y> const & r );</code></pre>
7280 <div class="listingblock">
7281 <div class="content">
7282 <pre class="highlight"><code>template<class Y> local_shared_ptr( shared_ptr<Y> && r );</code></pre>
7285 <div class="ulist none">
7291 <dt class="hdlist1">Effects</dt>
7293 <p>Constructs a <code>local_shared_ptr</code> that owns <code>r</code>.</p>
7295 <dt class="hdlist1">Postconditions</dt>
7297 <p><code>local_use_count() == 1</code>. <code>get()</code> returns the old value of <code>r.get()</code>.</p>
7299 <dt class="hdlist1">Throws</dt>
7301 <p><code>std::bad_alloc</code>, or an implementation-defined exception when a resource other than memory could not be obtained.</p>
7310 <h4 id="local_shared_ptr_aliasing_constructor">aliasing constructor</h4>
7311 <div class="listingblock">
7312 <div class="content">
7313 <pre class="highlight"><code>template<class Y> local_shared_ptr(local_shared_ptr<Y> const & r, element_type * p) noexcept;</code></pre>
7316 <div class="ulist none">
7322 <dt class="hdlist1">Effects</dt>
7324 <p>constructs a <code>local_shared_ptr</code> that shares ownership with <code>r</code> and stores <code>p</code>.</p>
7326 <dt class="hdlist1">Postconditions</dt>
7328 <p><code>get() == p && local_use_count() == r.local_use_count()</code>.</p>
7337 <h4 id="local_shared_ptr_aliasing_move_constructor">aliasing move constructor</h4>
7338 <div class="listingblock">
7339 <div class="content">
7340 <pre class="highlight"><code>template<class Y> local_shared_ptr(local_shared_ptr<Y> && r, element_type * p) noexcept;</code></pre>
7343 <div class="ulist none">
7349 <dt class="hdlist1">Effects</dt>
7351 <p>Move-constructs a <code>local_shared_ptr</code> from <code>r</code>, while storing <code>p</code> instead.</p>
7353 <dt class="hdlist1">Postconditions</dt>
7355 <p><code>get() == p</code> and <code>local_use_count()</code> equals the old count of <code>r</code>. <code>r</code> is empty and <code>r.get() == 0</code>.</p>
7364 <h4 id="local_shared_ptr_unique_ptr_constructor">unique_ptr constructor</h4>
7365 <div class="listingblock">
7366 <div class="content">
7367 <pre class="highlight"><code>template<class Y, class D> local_shared_ptr(std::unique_ptr<Y, D> && r);</code></pre>
7370 <div class="ulist none">
7376 <dt class="hdlist1">Requires</dt>
7378 <p><code>Y*</code> should be convertible to <code>T*</code>.</p>
7380 <dt class="hdlist1">Effects</dt>
7385 <p>When <code>r.get() == 0</code>, equivalent to <code>local_shared_ptr()</code>;</p>
7388 <p>Otherwise, constructs a <code>local_shared_ptr</code> that owns <code>shared_ptr<T>( std::move(r) )</code>.</p>
7393 <dt class="hdlist1">Throws</dt>
7395 <p><code>std::bad_alloc</code>, or an implementation-defined exception when a resource other than memory could not be obtained.</p>
7397 <dt class="hdlist1">Exception safety</dt>
7399 <p>If an exception is thrown, the constructor has no effect.</p>
7408 <h4 id="local_shared_ptr_destructor">destructor</h4>
7409 <div class="listingblock">
7410 <div class="content">
7411 <pre class="highlight"><code>~local_shared_ptr() noexcept;</code></pre>
7414 <div class="ulist none">
7420 <dt class="hdlist1">Effects</dt>
7425 <p>If <code>*this</code> is empty, or shares ownership with another <code>local_shared_ptr</code> instance (<code>local_use_count() > 1</code>), there are no side effects.</p>
7428 <p>Otherwise, destroys the owned <code>shared_ptr</code>.</p>
7440 <h4 id="local_shared_ptr_assignment">assignment</h4>
7441 <div class="listingblock">
7442 <div class="content">
7443 <pre class="highlight"><code>local_shared_ptr & operator=(local_shared_ptr const & r) noexcept;</code></pre>
7446 <div class="listingblock">
7447 <div class="content">
7448 <pre class="highlight"><code>template<class Y> local_shared_ptr & operator=(local_shared_ptr<Y> const & r) noexcept;</code></pre>
7451 <div class="ulist none">
7457 <dt class="hdlist1">Effects</dt>
7459 <p>Equivalent to <code>local_shared_ptr(r).swap(*this)</code>.</p>
7461 <dt class="hdlist1">Returns</dt>
7463 <p><code>*this</code>.</p>
7470 <div class="listingblock">
7471 <div class="content">
7472 <pre class="highlight"><code>local_shared_ptr & operator=(local_shared_ptr && r) noexcept;</code></pre>
7475 <div class="listingblock">
7476 <div class="content">
7477 <pre class="highlight"><code>template<class Y> local_shared_ptr & operator=(local_shared_ptr<Y> && r) noexcept;</code></pre>
7480 <div class="listingblock">
7481 <div class="content">
7482 <pre class="highlight"><code>template<class Y, class D> local_shared_ptr & operator=(std::unique_ptr<Y, D> && r);</code></pre>
7485 <div class="ulist none">
7491 <dt class="hdlist1">Effects</dt>
7493 <p>Equivalent to <code>local_shared_ptr(std::move(r)).swap(*this)</code>.</p>
7495 <dt class="hdlist1">Returns</dt>
7497 <p><code>*this</code>.</p>
7504 <div class="listingblock">
7505 <div class="content">
7506 <pre class="highlight"><code>local_shared_ptr & operator=(std::nullptr_t) noexcept;</code></pre>
7509 <div class="ulist none">
7515 <dt class="hdlist1">Effects</dt>
7517 <p>Equivalent to <code>local_shared_ptr().swap(*this)</code>.</p>
7519 <dt class="hdlist1">Returns</dt>
7521 <p><code>*this</code>.</p>
7530 <h4 id="local_shared_ptr_reset">reset</h4>
7531 <div class="listingblock">
7532 <div class="content">
7533 <pre class="highlight"><code>void reset() noexcept;</code></pre>
7536 <div class="ulist none">
7542 <dt class="hdlist1">Effects</dt>
7544 <p>Equivalent to <code>local_shared_ptr().swap(*this)</code>.</p>
7551 <div class="listingblock">
7552 <div class="content">
7553 <pre class="highlight"><code>template<class Y> void reset(Y * p);</code></pre>
7556 <div class="ulist none">
7562 <dt class="hdlist1">Effects</dt>
7564 <p>Equivalent to <code>local_shared_ptr(p).swap(*this)</code>.</p>
7571 <div class="listingblock">
7572 <div class="content">
7573 <pre class="highlight"><code>template<class Y, class D> void reset(Y * p, D d);</code></pre>
7576 <div class="ulist none">
7582 <dt class="hdlist1">Effects</dt>
7584 <p>Equivalent to <code>local_shared_ptr(p, d).swap(*this)</code>.</p>
7591 <div class="listingblock">
7592 <div class="content">
7593 <pre class="highlight"><code>template<class Y, class D, class A> void reset(Y * p, D d, A a);</code></pre>
7596 <div class="ulist none">
7602 <dt class="hdlist1">Effects</dt>
7604 <p>Equivalent to <code>local_shared_ptr(p, d, a).swap(*this)</code>.</p>
7611 <div class="listingblock">
7612 <div class="content">
7613 <pre class="highlight"><code>template<class Y> void reset(local_shared_ptr<Y> const & r, element_type * p) noexcept;</code></pre>
7616 <div class="ulist none">
7622 <dt class="hdlist1">Effects</dt>
7624 <p>Equivalent to <code>local_shared_ptr(r, p).swap(*this)</code>.</p>
7631 <div class="listingblock">
7632 <div class="content">
7633 <pre class="highlight"><code>template<class Y> void reset(local_shared_ptr<Y> && r, element_type * p) noexcept;</code></pre>
7636 <div class="ulist none">
7642 <dt class="hdlist1">Effects</dt>
7644 <p>Equivalent to <code>local_shared_ptr(std::move(r), p).swap(*this)</code>.</p>
7653 <h4 id="local_shared_ptr_indirection">indirection</h4>
7654 <div class="listingblock">
7655 <div class="content">
7656 <pre class="highlight"><code>T & operator*() const noexcept;</code></pre>
7659 <div class="ulist none">
7665 <dt class="hdlist1">Requires</dt>
7667 <p><code>T</code> should not be an array type.</p>
7669 <dt class="hdlist1">Returns</dt>
7671 <p><code>*get()</code>.</p>
7678 <div class="listingblock">
7679 <div class="content">
7680 <pre class="highlight"><code>T * operator->() const noexcept;</code></pre>
7683 <div class="ulist none">
7689 <dt class="hdlist1">Requires</dt>
7691 <p><code>T</code> should not be an array type.</p>
7693 <dt class="hdlist1">Returns</dt>
7695 <p><code>get()</code>.</p>
7702 <div class="listingblock">
7703 <div class="content">
7704 <pre class="highlight"><code>element_type & operator[](std::ptrdiff_t i) const noexcept;</code></pre>
7707 <div class="ulist none">
7713 <dt class="hdlist1">Requires</dt>
7715 <p><code>T</code> should be an array type. The stored pointer must not be 0. <code>i >= 0</code>. If <code>T</code> is <code>U[N]</code>, <code>i < N</code>.</p>
7717 <dt class="hdlist1">Returns</dt>
7719 <p><code>get()[i]</code>.</p>
7728 <h4 id="local_shared_ptr_get">get</h4>
7729 <div class="listingblock">
7730 <div class="content">
7731 <pre class="highlight"><code>element_type * get() const noexcept;</code></pre>
7734 <div class="ulist none">
7740 <dt class="hdlist1">Returns</dt>
7742 <p>The stored pointer.</p>
7751 <h4 id="local_shared_ptr_local_use_count">local_use_count</h4>
7752 <div class="listingblock">
7753 <div class="content">
7754 <pre class="highlight"><code>long local_use_count() const noexcept;</code></pre>
7757 <div class="ulist none">
7763 <dt class="hdlist1">Returns</dt>
7765 <p>The number of <code>local_shared_ptr</code> objects, <code>*this</code> included, that share ownership with <code>*this</code>, or 0 when <code>*this</code> is empty.</p>
7774 <h4 id="local_shared_ptr_conversions">conversions</h4>
7775 <div class="listingblock">
7776 <div class="content">
7777 <pre class="highlight"><code>explicit operator bool() const noexcept;</code></pre>
7780 <div class="ulist none">
7786 <dt class="hdlist1">Returns</dt>
7788 <p><code>get() != 0</code>.</p>
7795 <div class="admonitionblock note">
7799 <div class="title">Note</div>
7801 <td class="content">
7802 On C++03 compilers, the return value is of an unspecified type.
7807 <div class="listingblock">
7808 <div class="content">
7809 <pre class="highlight"><code>template<class Y> operator shared_ptr<Y>() const noexcept;</code></pre>
7812 <div class="listingblock">
7813 <div class="content">
7814 <pre class="highlight"><code>template<class Y> operator weak_ptr<Y>() const noexcept;</code></pre>
7817 <div class="ulist none">
7823 <dt class="hdlist1">Requires</dt>
7825 <p><code>T*</code> should be convertible to <code>Y*</code>.</p>
7827 <dt class="hdlist1">Returns</dt>
7829 <p>a copy of the owned <code>shared_ptr</code>.</p>
7838 <h4 id="local_shared_ptr_swap">swap</h4>
7839 <div class="listingblock">
7840 <div class="content">
7841 <pre class="highlight"><code>void swap(local_shared_ptr & b) noexcept;</code></pre>
7844 <div class="ulist none">
7850 <dt class="hdlist1">Effects</dt>
7852 <p>Exchanges the contents of the two smart pointers.</p>
7861 <h4 id="local_shared_ptr_owner_before">owner_before</h4>
7862 <div class="listingblock">
7863 <div class="content">
7864 <pre class="highlight"><code>template<class Y> bool owner_before(local_shared_ptr<Y> const & rhs) const noexcept;</code></pre>
7867 <div class="ulist none">
7873 <dt class="hdlist1">Effects</dt>
7875 <p>See the description of <code>operator<</code>.</p>
7885 <h3 id="local_shared_ptr_free_functions">Free Functions</h3>
7887 <h4 id="local_shared_ptr_comparison">comparison</h4>
7888 <div class="listingblock">
7889 <div class="content">
7890 <pre class="highlight"><code>template<class T, class U>
7891 bool operator==(local_shared_ptr<T> const & a, local_shared_ptr<U> const & b) noexcept;</code></pre>
7894 <div class="listingblock">
7895 <div class="content">
7896 <pre class="highlight"><code>template<class T, class U>
7897 bool operator==(local_shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept;</code></pre>
7900 <div class="listingblock">
7901 <div class="content">
7902 <pre class="highlight"><code>template<class T, class U>
7903 bool operator==(shared_ptr<T> const & a, local_shared_ptr<U> const & b) noexcept;</code></pre>
7906 <div class="ulist none">
7912 <dt class="hdlist1">Returns</dt>
7914 <p><code>a.get() == b.get()</code>.</p>
7921 <div class="listingblock">
7922 <div class="content">
7923 <pre class="highlight"><code>template<class T, class U>
7924 bool operator!=(local_shared_ptr<T> const & a, local_shared_ptr<U> const & b) noexcept;</code></pre>
7927 <div class="listingblock">
7928 <div class="content">
7929 <pre class="highlight"><code>template<class T, class U>
7930 bool operator!=(local_shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept;</code></pre>
7933 <div class="listingblock">
7934 <div class="content">
7935 <pre class="highlight"><code>template<class T, class U>
7936 bool operator!=(shared_ptr<T> const & a, local_shared_ptr<U> const & b) noexcept;</code></pre>
7939 <div class="ulist none">
7945 <dt class="hdlist1">Returns</dt>
7947 <p><code>a.get() != b.get()</code>.</p>
7954 <div class="listingblock">
7955 <div class="content">
7956 <pre class="highlight"><code>template<class T> bool operator==(local_shared_ptr<T> const & p, std::nullptr_t) noexcept;</code></pre>
7959 <div class="listingblock">
7960 <div class="content">
7961 <pre class="highlight"><code>template<class T> bool operator==(std::nullptr_t, local_shared_ptr<T> const & p) noexcept;</code></pre>
7964 <div class="ulist none">
7970 <dt class="hdlist1">Returns</dt>
7972 <p><code>p.get() == 0</code>.</p>
7979 <div class="listingblock">
7980 <div class="content">
7981 <pre class="highlight"><code>template<class T> bool operator!=(local_shared_ptr<T> const & p, std::nullptr_t) noexcept;</code></pre>
7984 <div class="listingblock">
7985 <div class="content">
7986 <pre class="highlight"><code>template<class T> bool operator!=(std::nullptr_t, local_shared_ptr<T> const & p) noexcept;</code></pre>
7989 <div class="ulist none">
7995 <dt class="hdlist1">Returns</dt>
7997 <p><code>p.get() != 0</code>.</p>
8004 <div class="listingblock">
8005 <div class="content">
8006 <pre class="highlight"><code>template<class T, class U>
8007 bool operator<(local_shared_ptr<T> const & a, local_shared_ptr<U> const & b) noexcept;</code></pre>
8010 <div class="ulist none">
8016 <dt class="hdlist1">Returns</dt>
8018 <p>An unspecified value such that</p>
8022 <p><code>operator<</code> is a strict weak ordering as described in section [lib.alg.sorting] of the C++ standard;</p>
8025 <p>under the equivalence relation defined by <code>operator<</code>, <code>!(a < b) && !(b < a)</code>, two <code>local_shared_ptr</code> instances
8026 are equivalent if and only if they share ownership or are both empty.</p>
8036 <div class="admonitionblock note">
8040 <div class="title">Note</div>
8042 <td class="content">
8043 Allows <code>local_shared_ptr</code> objects to be used as keys in associative containers.
8048 <div class="admonitionblock note">
8052 <div class="title">Note</div>
8054 <td class="content">
8055 The rest of the comparison operators are omitted by design.
8062 <h4 id="local_shared_ptr_swap_2">swap</h4>
8063 <div class="listingblock">
8064 <div class="content">
8065 <pre class="highlight"><code>template<class T> void swap(local_shared_ptr<T> & a, local_shared_ptr<T> & b) noexcept;</code></pre>
8068 <div class="ulist none">
8074 <dt class="hdlist1">Effects</dt>
8076 <p>Equivalent to <code>a.swap(b)</code>.</p>
8085 <h4 id="local_shared_ptr_get_pointer">get_pointer</h4>
8086 <div class="listingblock">
8087 <div class="content">
8088 <pre class="highlight"><code>template<class T>
8089 typename local_shared_ptr<T>::element_type *
8090 get_pointer(local_shared_ptr<T> const & p) noexcept;</code></pre>
8093 <div class="ulist none">
8099 <dt class="hdlist1">Returns</dt>
8101 <p><code>p.get()</code>.</p>
8108 <div class="admonitionblock note">
8112 <div class="title">Note</div>
8114 <td class="content">
8115 Provided as an aid to generic programming. Used by <code>mem_fn</code>.
8122 <h4 id="local_shared_ptr_static_pointer_cast">static_pointer_cast</h4>
8123 <div class="listingblock">
8124 <div class="content">
8125 <pre class="highlight"><code>template<class T, class U>
8126 local_shared_ptr<T> static_pointer_cast(local_shared_ptr<U> const & r) noexcept;</code></pre>
8129 <div class="ulist none">
8135 <dt class="hdlist1">Requires</dt>
8137 <p>The expression <code>static_cast<T*>( (U*)0 )</code> must be well-formed.</p>
8139 <dt class="hdlist1">Returns</dt>
8141 <p><code>local_shared_ptr<T>( r, static_cast<typename local_shared_ptr<T>::element_type*>(r.get()) )</code>.</p>
8148 <div class="admonitionblock caution">
8152 <div class="title">Caution</div>
8154 <td class="content">
8155 The seemingly equivalent expression <code>local_shared_ptr<T>(static_cast<T*>(r.get()))</code> will eventually
8156 result in undefined behavior, attempting to delete the same object twice.
8163 <h4 id="local_shared_ptr_const_pointer_cast">const_pointer_cast</h4>
8164 <div class="listingblock">
8165 <div class="content">
8166 <pre class="highlight"><code>template<class T, class U>
8167 local_shared_ptr<T> const_pointer_cast(local_shared_ptr<U> const & r) noexcept;</code></pre>
8170 <div class="ulist none">
8176 <dt class="hdlist1">Requires</dt>
8178 <p>The expression <code>const_cast<T*>( (U*)0 )</code> must be well-formed.</p>
8180 <dt class="hdlist1">Returns</dt>
8182 <p><code>local_shared_ptr<T>( r, const_cast<typename local_shared_ptr<T>::element_type*>(r.get()) )</code>.</p>
8191 <h4 id="local_shared_ptr_dynamic_pointer_cast">dynamic_pointer_cast</h4>
8192 <div class="listingblock">
8193 <div class="content">
8194 <pre class="highlight"><code>template<class T, class U>
8195 local_shared_ptr<T> dynamic_pointer_cast(local_shared_ptr<U> const & r) noexcept;</code></pre>
8198 <div class="ulist none">
8204 <dt class="hdlist1">Requires</dt>
8206 <p>The expression <code>dynamic_cast<T*>( (U*)0 )</code> must be well-formed.</p>
8208 <dt class="hdlist1">Returns</dt>
8213 <p>When <code>dynamic_cast<typename local_shared_ptr<T>::element_type*>(r.get())</code> returns a nonzero value <code>p</code>, <code>local_shared_ptr<T>(r, p)</code>;</p>
8216 <p>Otherwise, <code>local_shared_ptr<T>()</code>.</p>
8228 <h4 id="local_shared_ptr_reinterpret_pointer_cast">reinterpret_pointer_cast</h4>
8229 <div class="listingblock">
8230 <div class="content">
8231 <pre class="highlight"><code>template<class T, class U>
8232 local_shared_ptr<T> reinterpret_pointer_cast(local_shared_ptr<U> const & r) noexcept;</code></pre>
8235 <div class="ulist none">
8241 <dt class="hdlist1">Requires</dt>
8243 <p>The expression <code>reinterpret_cast<T*>( (U*)0 )</code> must be well-formed.</p>
8245 <dt class="hdlist1">Returns</dt>
8247 <p><code>local_shared_ptr<T>( r, reinterpret_cast<typename local_shared_ptr<T>::element_type*>(r.get()) )</code>.</p>
8256 <h4 id="local_shared_ptr_operator">operator<<</h4>
8257 <div class="listingblock">
8258 <div class="content">
8259 <pre class="highlight"><code>template<class E, class T, class Y>
8260 std::basic_ostream<E, T> &
8261 operator<< (std::basic_ostream<E, T> & os, local_shared_ptr<Y> const & p);</code></pre>
8264 <div class="ulist none">
8270 <dt class="hdlist1">Effects</dt>
8272 <p><code>os << p.get();</code>.</p>
8274 <dt class="hdlist1">Returns</dt>
8276 <p><code>os</code>.</p>
8285 <h4 id="local_shared_ptr_get_deleter">get_deleter</h4>
8286 <div class="listingblock">
8287 <div class="content">
8288 <pre class="highlight"><code>template<class D, class T>
8289 D * get_deleter(local_shared_ptr<T> const & p) noexcept;</code></pre>
8292 <div class="ulist none">
8298 <dt class="hdlist1">Returns</dt>
8300 <p>If <code>*this</code> owns a <code>shared_ptr</code> instance <code>p</code>, <code>get_deleter<D>( p )</code>, otherwise 0.</p>
8312 <h2 id="make_local_shared">make_local_shared: Creating local_shared_ptr</h2>
8313 <div class="sectionbody">
8315 <h3 id="make_local_shared_description">Description</h3>
8316 <div class="paragraph">
8317 <p>The function templates <code>make_local_shared</code> and <code>allocate_local_shared</code> provide
8318 convenient, safe and efficient ways to create <code>local_shared_ptr</code> objects. They
8319 are analogous to <code>make_shared</code> and <code>allocate_shared</code> for <code>shared_ptr</code>.</p>
8323 <h3 id="make_local_shared_synopsis">Synopsis</h3>
8324 <div class="paragraph">
8325 <p><code>make_local_shared</code> and <code>allocate_local_shared</code> are defined in
8326 <code><boost/smart_ptr/make_local_shared.hpp></code>.</p>
8328 <div class="listingblock">
8329 <div class="content">
8330 <pre class="highlight"><code>namespace boost {
8331 <code>// T is not an array</code>
8332 template<class T, class... Args>
8333 local_shared_ptr<T> make_local_shared(Args&&... args);
8334 template<class T, class A, class... Args>
8335 local_shared_ptr<T> allocate_local_shared(const A& a, Args&&... args);
8337 <code>// T is an array of unknown bounds</code>
8338 template<class T>
8339 local_shared_ptr<T> make_local_shared(std::size_t n);
8340 template<class T, class A>
8341 local_shared_ptr<T> allocate_local_shared(const A& a, std::size_t n);
8343 <code>// T is an array of known bounds</code>
8344 template<class T>
8345 local_shared_ptr<T> make_local_shared();
8346 template<class T, class A>
8347 local_shared_ptr<T> allocate_local_shared(const A& a);
8349 <code>// T is an array of unknown bounds</code>
8350 template<class T>
8351 local_shared_ptr<T> make_local_shared(std::size_t n,
8352 const remove_extent_t<T>& v);
8353 template<class T, class A>
8354 local_shared_ptr<T> allocate_local_shared(const A& a, std::size_t n,
8355 const remove_extent_t<T>& v);
8357 <code>// T is an array of known bounds</code>
8358 template<class T>
8359 local_shared_ptr<T> make_local_shared(const remove_extent_t<T>& v);
8360 template<class T, class A>
8361 local_shared_ptr<T> allocate_local_shared(const A& a,
8362 const remove_extent_t<T>& v);
8364 <code>// T is not an array of known bounds</code>
8365 template<class T>
8366 local_shared_ptr<T> make_local_shared_noinit();
8367 template<class T, class A>
8368 local_shared_ptr<T> allocate_local_shared_noinit(const A& a);
8370 <code>// T is an array of unknown bounds</code>
8371 template<class T>
8372 local_shared_ptr<T> make_local_shared_noinit(std::size_t n);
8373 template<class T, class A>
8374 local_shared_ptr<T> allocate_local_shared_noinit(const A& a,
8381 <h3 id="make_local_shared_description_2">Description</h3>
8382 <div class="paragraph">
8383 <p>The requirements and effects of these functions are the same as <code>make_shared</code>
8384 and <code>allocate_shared</code>, except that a <code>local_shared_ptr</code> is returned.</p>
8390 <h2 id="pointer_cast">Generic Pointer Casts</h2>
8391 <div class="sectionbody">
8393 <h3 id="pointer_cast_description">Description</h3>
8394 <div class="paragraph">
8395 <p>The pointer cast function templates (<code>static_pointer_cast</code>,
8396 <code>dynamic_pointer_cast</code>, <code>const_pointer_cast</code>, and <code>reinterpret_pointer_cast</code>)
8397 provide a way to write generic pointer castings for raw pointers,
8398 <code>std::shared_ptr</code> and <code>std::unique_ptr</code>.</p>
8400 <div class="paragraph">
8401 <p>There is test and example code in
8402 <a href="../../test/pointer_cast_test.cpp">pointer_cast_test.cpp</a></p>
8406 <h3 id="pointer_cast_rationale">Rationale</h3>
8407 <div class="paragraph">
8408 <p>Boost smart pointers usually overload those functions to provide a mechanism
8409 to emulate pointers casts. For example, <code>shared_ptr<T></code> implements a static
8410 pointer cast this way:</p>
8412 <div class="listingblock">
8413 <div class="content">
8414 <pre class="highlight"><code>template<class T, class U>
8415 shared_ptr<T> static_pointer_cast(const shared_ptr<U>& p);</code></pre>
8418 <div class="paragraph">
8419 <p>Pointer cast functions templates are overloads of <code>static_pointer_cast</code>,
8420 <code>dynamic_pointer_cast</code>, <code>const_pointer_cast</code>, and <code>reinterpret_pointer_cast</code>
8421 for raw pointers, <code>std::shared_ptr</code> and <code>std::unique_ptr</code>. This way when
8422 developing pointer type independent classes, for example, memory managers or
8423 shared memory compatible classes, the same code can be used for raw and smart
8428 <h3 id="pointer_cast_synopsis">Synopsis</h3>
8429 <div class="paragraph">
8430 <p>The generic pointer casts are defined in <code><boost/pointer_cast.hpp></code>.</p>
8432 <div class="listingblock">
8433 <div class="content">
8434 <pre class="highlight"><code>namespace boost {
8435 template<class T, class U> T* static_pointer_cast(U* p) noexcept;
8436 template<class T, class U> T* dynamic_pointer_cast(U* p) noexcept;
8437 template<class T, class U> T* const_pointer_cast(U* p) noexcept;
8438 template<class T, class U> T* reinterpret_pointer_cast(U* p) noexcept;
8440 template<class T, class U> std::shared_ptr<T>
8441 static_pointer_cast(const std::shared_ptr<U>& p) noexcept;
8442 template<class T, class U> std::shared_ptr<T>
8443 dynamic_pointer_cast(const std::shared_ptr<U>& p) noexcept;
8444 template<class T, class U> std::shared_ptr<T>
8445 const_pointer_cast(const std::shared_ptr<U>& p) noexcept;
8446 template<class T, class U> std::shared_ptr<T>
8447 reinterpret_pointer_cast(const std::shared_ptr<U>& p) noexcept;
8449 template<class T, class U> std::unique_ptr<T>
8450 static_pointer_cast(std::unique_ptr<U>&& p) noexcept;
8451 template<class T, class U> std::unique_ptr<T>
8452 dynamic_pointer_cast(std::unique_ptr<U>&& p) noexcept;
8453 template<class T, class U> std::unique_ptr<T>
8454 const_pointer_cast(std::unique_ptr<U>&& p) noexcept;
8455 template<class T, class U> std::unique_ptr<T>
8456 reinterpret_pointer_cast(std::unique_ptr<U>&& p) noexcept;
8462 <h3 id="pointer_cast_free_functions">Free Functions</h3>
8464 <h4 id="pointer_cast_static_pointer_cast">static_pointer_cast</h4>
8465 <div class="listingblock">
8466 <div class="content">
8467 <pre class="highlight"><code>template<class T, class U> T* static_pointer_cast(U* p) noexcept;</code></pre>
8470 <div class="ulist none">
8476 <dt class="hdlist1">Returns</dt>
8478 <p><code>static_cast<T*>(p)</code></p>
8485 <div class="listingblock">
8486 <div class="content">
8487 <pre class="highlight"><code>template<class T, class U> std::shared_ptr<T>
8488 static_pointer_cast(const std::shared_ptr<U>& p) noexcept;</code></pre>
8491 <div class="ulist none">
8497 <dt class="hdlist1">Returns</dt>
8499 <p><code>std::static_pointer_cast<T>(p)</code></p>
8506 <div class="listingblock">
8507 <div class="content">
8508 <pre class="highlight"><code>template<class T, class U> std::unique_ptr<T>
8509 static_pointer_cast(std::unique_ptr<U>&& p) noexcept;</code></pre>
8512 <div class="ulist none">
8518 <dt class="hdlist1">Requires</dt>
8520 <p>The expression <code>static_cast<T*>((U*)0)</code> must be well-formed.</p>
8522 <dt class="hdlist1">Returns</dt>
8524 <p><code>std::unique_ptr<T>(static_cast<typename
8525 std::unique_ptr<T>::element_type*>(p.release()))</code>.</p>
8532 <div class="admonitionblock caution">
8536 <div class="title">Caution</div>
8538 <td class="content">
8539 The seemingly equivalent expression
8540 <code>std::unique_ptr<T>(static_cast<T*>(p.get()))</code> will eventually result in
8541 undefined behavior, attempting to delete the same object twice.
8548 <h4 id="pointer_cast_dynamic_pointer_cast">dynamic_pointer_cast</h4>
8549 <div class="listingblock">
8550 <div class="content">
8551 <pre class="highlight"><code>template<class T, class U> T* dynamic_pointer_cast(U* p) noexcept;</code></pre>
8554 <div class="ulist none">
8560 <dt class="hdlist1">Returns</dt>
8562 <p><code>dynamic_cast<T*>(p)</code></p>
8569 <div class="listingblock">
8570 <div class="content">
8571 <pre class="highlight"><code>template<class T, class U> std::shared_ptr<T>
8572 dynamic_pointer_cast(const std::shared_ptr<U>& p) noexcept;</code></pre>
8575 <div class="ulist none">
8581 <dt class="hdlist1">Returns</dt>
8583 <p><code>std::dynamic_pointer_cast<T>(p)</code></p>
8590 <div class="listingblock">
8591 <div class="content">
8592 <pre class="highlight"><code>template<class T, class U> std::unique_ptr<T>
8593 dynamic_pointer_cast(std::unique_ptr<U>&& p) noexcept;</code></pre>
8596 <div class="ulist none">
8602 <dt class="hdlist1">Requires</dt>
8607 <p>The expression <code>static_cast<T*>((U*)0)</code> must be well-formed.</p>
8610 <p><code>T</code> must have a virtual destructor.</p>
8613 <dt class="hdlist1">Returns</dt>
8618 <p>When <code>dynamic_cast<typename std::unique_ptr<T>::element_type*>(p.get())</code>
8619 returns a non-zero value, <code>std::unique_ptr<T>(dynamic_cast<typename
8620 std::unique_ptr<T>::element_type*>(p.release()));</code>.</p>
8623 <p>Otherwise, <code>std::unique_ptr<T>()</code>.</p>
8629 <h4 id="pointer_cast_const_pointer_cast">const_pointer_cast</h4>
8630 <div class="listingblock">
8631 <div class="content">
8632 <pre class="highlight"><code>template<class T, class U> T* const_pointer_cast(U* p) noexcept;</code></pre>
8635 <div class="ulist none">
8641 <dt class="hdlist1">Returns</dt>
8643 <p><code>const_cast<T*>(p)</code></p>
8650 <div class="listingblock">
8651 <div class="content">
8652 <pre class="highlight"><code>template<class T, class U> std::shared_ptr<T>
8653 const_pointer_cast(const std::shared_ptr<U>& p) noexcept;</code></pre>
8656 <div class="ulist none">
8662 <dt class="hdlist1">Returns</dt>
8664 <p><code>std::const_pointer_cast<T>(p)</code></p>
8671 <div class="listingblock">
8672 <div class="content">
8673 <pre class="highlight"><code>template<class T, class U> std::unique_ptr<T>
8674 const_pointer_cast(std::unique_ptr<U>&& p) noexcept;</code></pre>
8677 <div class="ulist none">
8683 <dt class="hdlist1">Requires</dt>
8685 <p>The expression <code>const_cast<T*>((U*)0)</code> must be well-formed.</p>
8687 <dt class="hdlist1">Returns</dt>
8689 <p><code>std::unique_ptr<T>(const_cast<typename
8690 std::unique_ptr<T>::element_type*>(p.release()))</code>.</p>
8699 <h4 id="pointer_cast_reinterpret_pointer_cast">reinterpret_pointer_cast</h4>
8700 <div class="listingblock">
8701 <div class="content">
8702 <pre class="highlight"><code>template<class T, class U> T* reinterpret_pointer_cast(U* p) noexcept;</code></pre>
8705 <div class="ulist none">
8711 <dt class="hdlist1">Returns</dt>
8713 <p><code>reinterpret_cast<T*>(p)</code></p>
8720 <div class="listingblock">
8721 <div class="content">
8722 <pre class="highlight"><code>template<class T, class U> std::shared_ptr<T>
8723 reinterpret_pointer_cast(const std::shared_ptr<U>& p) noexcept;</code></pre>
8726 <div class="ulist none">
8732 <dt class="hdlist1">Returns</dt>
8734 <p><code>std::reinterpret_pointer_cast<T>(p)</code></p>
8741 <div class="listingblock">
8742 <div class="content">
8743 <pre class="highlight"><code>template<class T, class U> std::unique_ptr<T>
8744 reinterpret_pointer_cast(std::unique_ptr<U>&& p) noexcept;</code></pre>
8747 <div class="ulist none">
8753 <dt class="hdlist1">Requires</dt>
8755 <p>The expression <code>reinterpret_cast<T*>((U*)0)</code> must be well-formed.</p>
8757 <dt class="hdlist1">Returns</dt>
8759 <p><code>std::unique_ptr<T>(reinterpret_cast<typename
8760 std::unique_ptr<T>::element_type*>(p.release()))</code>.</p>
8770 <h3 id="pointer_cast_example">Example</h3>
8771 <div class="paragraph">
8772 <p>The following example demonstrates how the generic pointer casts help us
8773 create pointer independent code.</p>
8775 <div class="listingblock">
8776 <div class="content">
8777 <pre class="highlight"><code>#include <boost/pointer_cast.hpp>
8778 #include <boost/shared_ptr.hpp>
8785 class derived : public base { };
8787 template<class Ptr>
8788 void check_if_it_is_derived(const Ptr& ptr)
8790 assert(boost::dynamic_pointer_cast<derived>(ptr) != 0);
8795 base* ptr = new derived;
8796 boost::shared_ptr<base> sptr(new derived);
8798 check_if_it_is_derived(ptr);
8799 check_if_it_is_derived(sptr);
8809 <h2 id="pointer_to_other">pointer_to_other</h2>
8810 <div class="sectionbody">
8812 <h3 id="pointer_to_other_description">Description</h3>
8813 <div class="paragraph">
8814 <p>The <code>pointer_to_other</code> utility provides a way, given a source pointer type, to obtain a pointer of the same type
8815 to another pointee type.</p>
8817 <div class="paragraph">
8818 <p>There is test/example code in <a href="../../test/pointer_to_other_test.cpp">pointer_to_other_test.cpp</a>.</p>
8822 <h3 id="pointer_to_other_rationale">Rationale</h3>
8823 <div class="paragraph">
8824 <p>When building pointer independent classes, like memory managers, allocators, or containers, there is often a need to
8825 define pointers generically, so that if a template parameter represents a pointer (for example, a raw or smart pointer
8826 to an <code>int</code>), we can define another pointer of the same type to another pointee (a raw or smart pointer to a <code>float</code>.)</p>
8828 <div class="listingblock">
8829 <div class="content">
8830 <pre class="highlight"><code>template <class IntPtr> class FloatPointerHolder
8832 // Let's define a pointer to a float
8834 typedef typename boost::pointer_to_other
8835 <IntPtr, float>::type float_ptr_t;
8837 float_ptr_t float_ptr;
8843 <h3 id="pointer_to_other_synopsis">Synopsis</h3>
8844 <div class="paragraph">
8845 <p><code>pointer_to_other</code> is defined in <code><boost/smart_ptr/pointer_to_other.hpp></code>.</p>
8847 <div class="listingblock">
8848 <div class="content">
8849 <pre class="highlight"><code>namespace boost {
8851 template<class T, class U> struct pointer_to_other;
8853 template<class T, class U,
8854 template <class> class Sp>
8855 struct pointer_to_other< Sp<T>, U >
8857 typedef Sp<U> type;
8860 template<class T, class T2, class U,
8861 template <class, class> class Sp>
8862 struct pointer_to_other< Sp<T, T2>, U >
8864 typedef Sp<U, T2> type;
8867 template<class T, class T2, class T3, class U,
8868 template <class, class, class> class Sp>
8869 struct pointer_to_other< Sp<T, T2, T3>, U >
8871 typedef Sp<U, T2, T3> type;
8874 template<class T, class U>
8875 struct pointer_to_other< T*, U >
8882 <div class="paragraph">
8883 <p>If these definitions are not correct for a specific smart pointer, we can define a specialization of <code>pointer_to_other</code>.</p>
8887 <h3 id="pointer_to_other_example">Example</h3>
8888 <div class="listingblock">
8889 <div class="content">
8890 <pre class="highlight"><code>// Let's define a memory allocator that can
8891 // work with raw and smart pointers
8893 #include <boost/pointer_to_other.hpp>
8895 template <class VoidPtr>
8896 class memory_allocator
8898 // Predefine a memory_block
8902 // Define a pointer to a memory_block from a void pointer
8903 // If VoidPtr is void *, block_ptr_t is block*
8904 // If VoidPtr is smart_ptr<void>, block_ptr_t is smart_ptr<block>
8906 typedef typename boost::pointer_to_other
8907 <VoidPtr, block>::type block_ptr_t;
8912 block_ptr_t next_block;
8915 block_ptr_t free_blocks;
8919 <div class="paragraph">
8920 <p>As we can see, using <code>pointer_to_other</code> we can create pointer independent code.</p>
8926 <h2 id="atomic_shared_ptr">atomic_shared_ptr</h2>
8927 <div class="sectionbody">
8929 <h3 id="atomic_shared_ptr_description">Description</h3>
8930 <div class="paragraph">
8931 <p>The class template <code>atomic_shared_ptr<T></code> implements the interface of <code>std::atomic</code>
8932 for a contained value of type <code>shared_ptr<T></code>. Concurrent access to <code>atomic_shared_ptr</code>
8933 is not a data race.</p>
8937 <h3 id="atomic_shared_ptr_synopsis">Synopsis</h3>
8938 <div class="paragraph">
8939 <p><code>atomic_shared_ptr</code> is defined in <code><boost/smart_ptr/atomic_shared_ptr.hpp></code>.</p>
8941 <div class="listingblock">
8942 <div class="content">
8943 <pre class="highlight"><code>namespace boost {
8945 template<class T> class atomic_shared_ptr {
8948 shared_ptr<T> p_; // exposition only
8950 atomic_shared_ptr(const atomic_shared_ptr&) = delete;
8951 atomic_shared_ptr& operator=(const atomic_shared_ptr&) = delete;
8955 constexpr atomic_shared_ptr() noexcept;
8956 atomic_shared_ptr( shared_ptr<T> p ) noexcept;
8958 atomic_shared_ptr& operator=( shared_ptr<T> r ) noexcept;
8960 bool is_lock_free() const noexcept;
8962 shared_ptr<T> load( int = 0 ) const noexcept;
8963 operator shared_ptr<T>() const noexcept;
8965 void store( shared_ptr<T> r, int = 0 ) noexcept;
8967 shared_ptr<T> exchange( shared_ptr<T> r, int = 0 ) noexcept;
8969 bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, int, int ) noexcept;
8970 bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, int = 0 ) noexcept;
8971 bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, int, int ) noexcept;
8972 bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, int = 0 ) noexcept;
8974 bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, int, int ) noexcept;
8975 bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, int = 0 ) noexcept;
8976 bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, int, int ) noexcept;
8977 bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, int = 0 ) noexcept;
8984 <h3 id="atomic_shared_ptr_members">Members</h3>
8985 <div class="listingblock">
8986 <div class="content">
8987 <pre class="highlight"><code>constexpr atomic_shared_ptr() noexcept;</code></pre>
8990 <div class="ulist none">
8996 <dt class="hdlist1">Effects</dt>
8998 <p>Default-initializes <code>p_</code>.</p>
9005 <div class="listingblock">
9006 <div class="content">
9007 <pre class="highlight"><code>atomic_shared_ptr( shared_ptr<T> p ) noexcept;</code></pre>
9010 <div class="ulist none">
9016 <dt class="hdlist1">Effects</dt>
9018 <p>Initializes <code>p_</code> to <code>p</code>.</p>
9025 <div class="listingblock">
9026 <div class="content">
9027 <pre class="highlight"><code>atomic_shared_ptr& operator=( shared_ptr<T> r ) noexcept;</code></pre>
9030 <div class="ulist none">
9036 <dt class="hdlist1">Effects</dt>
9038 <p><code>p_.swap(r)</code>.</p>
9040 <dt class="hdlist1">Returns</dt>
9042 <p><code>*this</code>.</p>
9049 <div class="listingblock">
9050 <div class="content">
9051 <pre class="highlight"><code>bool is_lock_free() const noexcept;</code></pre>
9054 <div class="ulist none">
9060 <dt class="hdlist1">Returns</dt>
9062 <p><code>false</code>.</p>
9069 <div class="admonitionblock note">
9073 <div class="title">Note</div>
9075 <td class="content">
9076 This implementation is not lock-free.
9081 <div class="listingblock">
9082 <div class="content">
9083 <pre class="highlight"><code>shared_ptr<T> load( int = 0 ) const noexcept;</code></pre>
9086 <div class="listingblock">
9087 <div class="content">
9088 <pre class="highlight"><code>operator shared_ptr<T>() const noexcept;</code></pre>
9091 <div class="ulist none">
9097 <dt class="hdlist1">Returns</dt>
9099 <p><code>p_</code>.</p>
9106 <div class="admonitionblock note">
9110 <div class="title">Note</div>
9112 <td class="content">
9113 The <code>int</code> argument is intended to be of type <code>memory_order</code>, but is ignored.
9114 This implementation is lock-based and therefore always sequentially consistent.
9119 <div class="listingblock">
9120 <div class="content">
9121 <pre class="highlight"><code>void store( shared_ptr<T> r, int = 0 ) noexcept;</code></pre>
9124 <div class="ulist none">
9130 <dt class="hdlist1">Effects</dt>
9132 <p><code>p_.swap(r)</code>.</p>
9139 <div class="listingblock">
9140 <div class="content">
9141 <pre class="highlight"><code>shared_ptr<T> exchange( shared_ptr<T> r, int = 0 ) noexcept;</code></pre>
9144 <div class="ulist none">
9150 <dt class="hdlist1">Effects</dt>
9152 <p><code>p_.swap(r)</code>.</p>
9154 <dt class="hdlist1">Returns</dt>
9156 <p>The old value of <code>p_</code>.</p>
9163 <div class="listingblock">
9164 <div class="content">
9165 <pre class="highlight"><code>bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, int, int ) noexcept;</code></pre>
9168 <div class="listingblock">
9169 <div class="content">
9170 <pre class="highlight"><code>bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, int = 0 ) noexcept;</code></pre>
9173 <div class="listingblock">
9174 <div class="content">
9175 <pre class="highlight"><code>bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, int, int ) noexcept;</code></pre>
9178 <div class="listingblock">
9179 <div class="content">
9180 <pre class="highlight"><code>bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, int = 0 ) noexcept;</code></pre>
9183 <div class="ulist none">
9189 <dt class="hdlist1">Effects</dt>
9191 <p>If <code>p_</code> is equivalent to <code>v</code>, assigns <code>w</code> to <code>p_</code>, otherwise assigns <code>p_</code> to <code>v</code>.</p>
9193 <dt class="hdlist1">Returns</dt>
9195 <p><code>true</code> if <code>p_</code> was equivalent to <code>v</code>, <code>false</code> otherwise.</p>
9197 <dt class="hdlist1">Remarks</dt>
9199 <p>Two <code>shared_ptr</code> instances are equivalent if they store the same pointer value and <em>share ownership</em>.</p>
9206 <div class="listingblock">
9207 <div class="content">
9208 <pre class="highlight"><code>bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, int, int ) noexcept;</code></pre>
9211 <div class="listingblock">
9212 <div class="content">
9213 <pre class="highlight"><code>bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, int = 0 ) noexcept;</code></pre>
9216 <div class="listingblock">
9217 <div class="content">
9218 <pre class="highlight"><code>bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, int, int ) noexcept;</code></pre>
9221 <div class="listingblock">
9222 <div class="content">
9223 <pre class="highlight"><code>bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, int = 0 ) noexcept;</code></pre>
9226 <div class="ulist none">
9232 <dt class="hdlist1">Effects</dt>
9234 <p>If <code>p_</code> is equivalent to <code>v</code>, assigns <code>std::move(w)</code> to <code>p_</code>, otherwise assigns <code>p_</code> to <code>v</code>.</p>
9236 <dt class="hdlist1">Returns</dt>
9238 <p><code>true</code> if <code>p_</code> was equivalent to <code>v</code>, <code>false</code> otherwise.</p>
9240 <dt class="hdlist1">Remarks</dt>
9242 <p>The old value of <code>w</code> is not preserved in either case.</p>
9253 <h2 id="techniques">Appendix A: Smart Pointer Programming Techniques</h2>
9254 <div class="sectionbody">
9256 <h3 id="techniques_incomplete">Using incomplete classes for implementation hiding</h3>
9257 <div class="paragraph">
9258 <p>A proven technique (that works in C, too) for separating interface from implementation is to use a pointer to an incomplete class as an opaque handle:</p>
9260 <div class="listingblock">
9261 <div class="content">
9262 <pre class="highlight"><code>class FILE;
9264 FILE * fopen(char const * name, char const * mode);
9265 void fread(FILE * f, void * data, size_t size);
9266 void fclose(FILE * f);</code></pre>
9269 <div class="paragraph">
9270 <p>It is possible to express the above interface using <code>shared_ptr</code>, eliminating the need to manually call <code>fclose</code>:</p>
9272 <div class="listingblock">
9273 <div class="content">
9274 <pre class="highlight"><code>class FILE;
9276 shared_ptr<FILE> fopen(char const * name, char const * mode);
9277 void fread(shared_ptr<FILE> f, void * data, size_t size);</code></pre>
9280 <div class="paragraph">
9281 <p>This technique relies on <code>shared_ptr</code>’s ability to execute a custom deleter, eliminating the explicit call to <code>fclose</code>, and on the fact that <code>shared_ptr<X></code> can be copied and destroyed when <code>X</code> is incomplete.</p>
9285 <h3 id="techniques_the_pimpl_idiom">The "Pimpl" idiom</h3>
9286 <div class="paragraph">
9287 <p>A C++ specific variation of the incomplete class pattern is the "Pimpl" idiom. The incomplete class is not exposed to the user; it is hidden behind a forwarding facade. <code>shared_ptr</code> can be used to implement a "Pimpl":</p>
9289 <div class="listingblock">
9290 <div class="content">
9291 <pre class="highlight"><code>// file.hpp:
9298 shared_ptr<impl> pimpl_;
9302 file(char const * name, char const * mode);
9304 // compiler generated members are fine and useful
9306 void read(void * data, size_t size);
9317 impl(impl const &);
9318 impl & operator=(impl const &);
9324 impl(char const * name, char const * mode) { ... }
9326 void read(void * data, size_t size) { ... }
9329 file::file(char const * name, char const * mode): pimpl_(new impl(name, mode))
9333 void file::read(void * data, size_t size)
9335 pimpl_->read(data, size);
9339 <div class="paragraph">
9340 <p>The key thing to note here is that the compiler-generated copy constructor, assignment operator, and destructor all have a sensible meaning. As a result, <code>file</code> is <code>CopyConstructible</code> and <code>Assignable</code>, allowing its use in standard containers.</p>
9344 <h3 id="techniques_using_abstract_classes_for_implementation_hiding">Using abstract classes for implementation hiding</h3>
9345 <div class="paragraph">
9346 <p>Another widely used C++ idiom for separating inteface and implementation is to use abstract base classes and factory functions.
9347 The abstract classes are sometimes called "interfaces" and the pattern is known as "interface-based programming". Again,
9348 <code>shared_ptr</code> can be used as the return type of the factory functions:</p>
9350 <div class="listingblock">
9351 <div class="content">
9352 <pre class="highlight"><code>// X.hpp:
9358 virtual void f() = 0;
9359 virtual void g() = 0;
9366 shared_ptr<X> createX();
9370 class X_impl: public X
9374 X_impl(X_impl const &);
9375 X_impl & operator=(X_impl const &);
9390 shared_ptr<X> createX()
9392 shared_ptr<X> px(new X_impl);
9397 <div class="paragraph">
9398 <p>A key property of <code>shared_ptr</code> is that the allocation, construction, deallocation, and destruction details are captured at the point of construction, inside the factory function.</p>
9400 <div class="paragraph">
9401 <p>Note the protected and nonvirtual destructor in the example above. The client code cannot, and does not need to, delete a pointer to <code>X</code>; the <code>shared_ptr<X></code> instance returned from <code>createX</code> will correctly call <code>~X_impl</code>.</p>
9405 <h3 id="techniques_preventing_delete_px_get">Preventing <code>delete px.get()</code></h3>
9406 <div class="paragraph">
9407 <p>It is often desirable to prevent client code from deleting a pointer that is being managed by <code>shared_ptr</code>. The previous technique showed one possible approach, using a protected destructor. Another alternative is to use a private deleter:</p>
9409 <div class="listingblock">
9410 <div class="content">
9411 <pre class="highlight"><code>class X
9418 friend class deleter;
9424 void operator()(X * p) { delete p; }
9429 static shared_ptr<X> create()
9431 shared_ptr<X> px(new X, X::deleter());
9439 <h3 id="techniques_encapsulating_allocation_details_wrapping_factory_functions">Encapsulating allocation details, wrapping factory functions</h3>
9440 <div class="paragraph">
9441 <p><code>shared_ptr</code> can be used in creating C++ wrappers over existing C style library interfaces that return raw pointers from their factory functions
9442 to encapsulate allocation details. As an example, consider this interface, where <code>CreateX</code> might allocate <code>X</code> from its own private heap, <code>~X</code> may
9443 be inaccessible, or <code>X</code> may be incomplete:</p>
9445 <div class="literalblock">
9446 <div class="content">
9448 void DestroyX(X *);</pre>
9451 <div class="paragraph">
9452 <p>The only way to reliably destroy a pointer returned by <code>CreateX</code> is to call <code>DestroyX</code>.</p>
9454 <div class="paragraph">
9455 <p>Here is how a <code>shared_ptr</code>-based wrapper may look like:</p>
9457 <div class="literalblock">
9458 <div class="content">
9459 <pre>shared_ptr<X> createX()
9461 shared_ptr<X> px(CreateX(), DestroyX);
9466 <div class="paragraph">
9467 <p>Client code that calls <code>createX</code> still does not need to know how the object has been allocated, but now the destruction is automatic.</p>
9471 <h3 id="techniques_static">Using a shared_ptr to hold a pointer to a statically allocated object</h3>
9472 <div class="paragraph">
9473 <p>Sometimes it is desirable to create a <code>shared_ptr</code> to an already existing object, so that the <code>shared_ptr</code> does not attempt to destroy the
9474 object when there are no more references left. As an example, the factory function:</p>
9476 <div class="literalblock">
9477 <div class="content">
9478 <pre>shared_ptr<X> createX();</pre>
9481 <div class="paragraph">
9482 <p>in certain situations may need to return a pointer to a statically allocated <code>X</code> instance.</p>
9484 <div class="paragraph">
9485 <p>The solution is to use a custom deleter that does nothing:</p>
9487 <div class="listingblock">
9488 <div class="content">
9489 <pre class="highlight"><code>struct null_deleter
9491 void operator()(void const *) const
9498 shared_ptr<X> createX()
9500 shared_ptr<X> px(&x, null_deleter());
9505 <div class="paragraph">
9506 <p>The same technique works for any object known to outlive the pointer.</p>
9510 <h3 id="techniques_using_a_shared_ptr_to_hold_a_pointer_to_a_com_object">Using a shared_ptr to hold a pointer to a COM Object</h3>
9511 <div class="paragraph">
9512 <p>Background: COM objects have an embedded reference count and two member functions that manipulate it. <code>AddRef()</code> increments the count.
9513 <code>Release()</code> decrements the count and destroys itself when the count drops to zero.</p>
9515 <div class="paragraph">
9516 <p>It is possible to hold a pointer to a COM object in a <code>shared_ptr</code>:</p>
9518 <div class="literalblock">
9519 <div class="content">
9520 <pre>shared_ptr<IWhatever> make_shared_from_COM(IWhatever * p)
9523 shared_ptr<IWhatever> pw(p, mem_fn(&IWhatever::Release));
9528 <div class="paragraph">
9529 <p>Note, however, that <code>shared_ptr</code> copies created from <code>pw</code> will not "register" in the embedded count of the COM object;
9530 they will share the single reference created in <code>make_shared_from_COM</code>. Weak pointers created from <code>pw</code> will be invalidated when the last
9531 <code>shared_ptr</code> is destroyed, regardless of whether the COM object itself is still alive.</p>
9533 <div class="paragraph">
9534 <p>As <a href="../../../../libs/bind/mem_fn.html#Q3">explained</a> in the <code>mem_fn</code> documentation, you need to <code>#define BOOST_MEM_FN_ENABLE_STDCALL</code> first.</p>
9538 <h3 id="techniques_intrusive">Using a shared_ptr to hold a pointer to an object with an embedded reference count</h3>
9539 <div class="paragraph">
9540 <p>This is a generalization of the above technique. The example assumes that the object implements the two functions required by <code><a href="#intrusive_ptr">intrusive_ptr</a></code>,
9541 <code>intrusive_ptr_add_ref</code> and <code>intrusive_ptr_release</code>:</p>
9543 <div class="listingblock">
9544 <div class="content">
9545 <pre class="highlight"><code>template<class T> struct intrusive_deleter
9547 void operator()(T * p)
9549 if(p) intrusive_ptr_release(p);
9553 shared_ptr<X> make_shared_from_intrusive(X * p)
9555 if(p) intrusive_ptr_add_ref(p);
9556 shared_ptr<X> px(p, intrusive_deleter<X>());
9563 <h3 id="techniques_using_a_shared_ptr_to_hold_another_shared_ownership_smart_pointer">Using a shared_ptr to hold another shared ownership smart pointer</h3>
9564 <div class="paragraph">
9565 <p>One of the design goals of <code>shared_ptr</code> is to be used in library interfaces. It is possible to encounter a situation where a library takes a
9566 <code>shared_ptr</code> argument, but the object at hand is being managed by a different reference counted or linked smart pointer.</p>
9568 <div class="paragraph">
9569 <p>It is possible to exploit <code>shared_ptr</code>’s custom deleter feature to wrap this existing smart pointer behind a <code>shared_ptr</code> facade:</p>
9571 <div class="listingblock">
9572 <div class="content">
9573 <pre class="highlight"><code>template<class P> struct smart_pointer_deleter
9581 smart_pointer_deleter(P const & p): p_(p)
9585 void operator()(void const *)
9590 P const & get() const
9596 shared_ptr<X> make_shared_from_another(another_ptr<X> qx)
9598 shared_ptr<X> px(qx.get(), smart_pointer_deleter< another_ptr<X> >(qx));
9603 <div class="paragraph">
9604 <p>One subtle point is that deleters are not allowed to throw exceptions, and the above example as written assumes that <code>p_.reset()</code> doesn’t throw.
9605 If this is not the case, <code>p_.reset();</code> should be wrapped in a <code>try {} catch(…​) {}</code> block that ignores exceptions. In the (usually unlikely) event
9606 when an exception is thrown and ignored, <code>p_</code> will be released when the lifetime of the deleter ends. This happens when all references, including
9607 weak pointers, are destroyed or reset.</p>
9609 <div class="paragraph">
9610 <p>Another twist is that it is possible, given the above <code>shared_ptr</code> instance, to recover the original smart pointer, using <code><a href="#shared_ptr_get_deleter">get_deleter</a></code>:</p>
9612 <div class="listingblock">
9613 <div class="content">
9614 <pre class="highlight"><code>void extract_another_from_shared(shared_ptr<X> px)
9616 typedef smart_pointer_deleter< another_ptr<X> > deleter;
9618 if(deleter const * pd = get_deleter<deleter>(px))
9620 another_ptr<X> qx = pd->get();
9631 <h3 id="techniques_from_raw">Obtaining a shared_ptr from a raw pointer</h3>
9632 <div class="paragraph">
9633 <p>Sometimes it is necessary to obtain a <code>shared_ptr</code> given a raw pointer to an object that is already managed by another <code>shared_ptr</code> instance. Example:</p>
9635 <div class="literalblock">
9636 <div class="content">
9639 shared_ptr<X> px(???);
9643 <div class="paragraph">
9644 <p>Inside <code>f</code>, we’d like to create a <code>shared_ptr</code> to <code>*p</code>.</p>
9646 <div class="paragraph">
9647 <p>In the general case, this problem has no solution. One approach is to modify <code>f</code> to take a <code>shared_ptr</code>, if possible:</p>
9649 <div class="literalblock">
9650 <div class="content">
9651 <pre>void f(shared_ptr<X> px);</pre>
9654 <div class="paragraph">
9655 <p>The same transformation can be used for nonvirtual member functions, to convert the implicit <code>this</code>:</p>
9657 <div class="literalblock">
9658 <div class="content">
9659 <pre>void X::f(int m);</pre>
9662 <div class="paragraph">
9663 <p>would become a free function with a <code>shared_ptr</code> first argument:</p>
9665 <div class="literalblock">
9666 <div class="content">
9667 <pre>void f(shared_ptr<X> this_, int m);</pre>
9670 <div class="paragraph">
9671 <p>If <code>f</code> cannot be changed, but <code>X</code> uses intrusive counting, use <code><a href="#techniques_intrusive">make_shared_from_intrusive</a></code> described above. Or, if it’s known that the <code>shared_ptr</code> created in <code>f</code> will never outlive the object, use <a href="#techniques_static">a null deleter</a>.</p>
9675 <h3 id="techniques_obtaining_a_shared_ptr_weak_ptr_to_this_in_a_constructor">Obtaining a shared_ptr (weak_ptr) to this in a constructor</h3>
9676 <div class="paragraph">
9677 <p>Some designs require objects to register themselves on construction with a central authority. When the registration routines take a <code>shared_ptr</code>, this leads to the question how could a constructor obtain a <code>shared_ptr</code> to <code>this</code>:</p>
9679 <div class="listingblock">
9680 <div class="content">
9681 <pre class="highlight"><code>class X
9687 shared_ptr<X> this_(???);
9692 <div class="paragraph">
9693 <p>In the general case, the problem cannot be solved. The <code>X</code> instance being constructed can be an automatic variable or a static variable; it can be created on the heap:</p>
9695 <div class="literalblock">
9696 <div class="content">
9697 <pre>shared_ptr<X> px(new X);</pre>
9700 <div class="paragraph">
9701 <p>but at construction time, <code>px</code> does not exist yet, and it is impossible to create another <code>shared_ptr</code> instance that shares ownership with it.</p>
9703 <div class="paragraph">
9704 <p>Depending on context, if the inner <code>shared_ptr this_</code> doesn’t need to keep the object alive, use a <code>null_deleter</code> as explained <a href="#techniques_static">here</a> and <a href="#techniques_weak_without_shared">here</a>.
9705 If <code>X</code> is supposed to always live on the heap, and be managed by a <code>shared_ptr</code>, use a static factory function:</p>
9707 <div class="listingblock">
9708 <div class="content">
9709 <pre class="highlight"><code>class X
9717 static shared_ptr<X> create()
9719 shared_ptr<X> px(new X);
9720 // use px as 'this_'
9728 <h3 id="techniques_obtaining_a_shared_ptr_to_this">Obtaining a shared_ptr to this</h3>
9729 <div class="paragraph">
9730 <p>Sometimes it is needed to obtain a <code>shared_ptr</code> from <code>this</code> in a virtual member function under the assumption that <code>this</code> is already managed by a <code>shared_ptr</code>.
9731 The transformations <a href="#techniques_from_raw">described in the previous technique</a> cannot be applied.</p>
9733 <div class="paragraph">
9734 <p>A typical example:</p>
9736 <div class="listingblock">
9737 <div class="content">
9738 <pre class="highlight"><code>class X
9742 virtual void f() = 0;
9753 virtual shared_ptr<X> getX() = 0;
9762 class impl: public X, public Y
9768 virtual void f() { ... }
9770 virtual shared_ptr<X> getX()
9772 shared_ptr<X> px(???);
9778 <div class="paragraph">
9779 <p>The solution is to keep a weak pointer to <code>this</code> as a member in <code>impl</code>:</p>
9781 <div class="listingblock">
9782 <div class="content">
9783 <pre class="highlight"><code>class impl: public X, public Y
9787 weak_ptr<impl> weak_this;
9789 impl(impl const &);
9790 impl & operator=(impl const &);
9796 static shared_ptr<impl> create()
9798 shared_ptr<impl> pi(new impl);
9799 pi->weak_this = pi;
9803 virtual void f() { ... }
9805 virtual shared_ptr<X> getX()
9807 shared_ptr<X> px(weak_this);
9813 <div class="paragraph">
9814 <p>The library now includes a helper class template <code><a href="#enable_shared_from_this">enable_shared_from_this</a></code> that can be used to encapsulate the solution:</p>
9816 <div class="listingblock">
9817 <div class="content">
9818 <pre class="highlight"><code>class impl: public X, public Y, public enable_shared_from_this<impl>
9822 impl(impl const &);
9823 impl & operator=(impl const &);
9827 virtual void f() { ... }
9829 virtual shared_ptr<X> getX()
9831 return shared_from_this();
9836 <div class="paragraph">
9837 <p>Note that you no longer need to manually initialize the <code>weak_ptr</code> member in <code>enable_shared_from_this</code>. Constructing a <code>shared_ptr</code> to <code>impl</code> takes care of that.</p>
9841 <h3 id="techniques_using_shared_ptr_as_a_smart_counted_handle">Using shared_ptr as a smart counted handle</h3>
9842 <div class="paragraph">
9843 <p>Some library interfaces use opaque handles, a variation of the <a href="#techniques_incomplete">incomplete class technique</a> described above. An example:</p>
9845 <div class="listingblock">
9846 <div class="content">
9847 <pre class="highlight"><code>typedef void * HANDLE;
9849 HANDLE CreateProcess();
9850 void CloseHandle(HANDLE);</code></pre>
9853 <div class="paragraph">
9854 <p>Instead of a raw pointer, it is possible to use <code>shared_ptr</code> as the handle and get reference counting and automatic resource management for free:</p>
9856 <div class="listingblock">
9857 <div class="content">
9858 <pre class="highlight"><code>typedef shared_ptr<void> handle;
9860 handle createProcess()
9862 shared_ptr<void> pv(CreateProcess(), CloseHandle);
9869 <h3 id="techniques_using_shared_ptr_to_execute_code_on_block_exit">Using shared_ptr to execute code on block exit</h3>
9870 <div class="paragraph">
9871 <p><code>shared_ptr<void></code> can automatically execute cleanup code when control leaves a scope.</p>
9876 <p>Executing <code>f(p)</code>, where <code>p</code> is a pointer:</p>
9877 <div class="listingblock">
9878 <div class="content">
9879 <pre class="highlight"><code>shared_ptr<void> guard(p, f);</code></pre>
9884 <p>Executing arbitrary code: <code>f(x, y)</code>:</p>
9885 <div class="listingblock">
9886 <div class="content">
9887 <pre class="highlight"><code>shared_ptr<void> guard(static_cast<void*>(0), bind(f, x, y));</code></pre>
9895 <h3 id="techniques_using_shared_ptrvoid_to_hold_an_arbitrary_object">Using shared_ptr<void> to hold an arbitrary object</h3>
9896 <div class="paragraph">
9897 <p><code>shared_ptr<void></code> can act as a generic object pointer similar to <code>void*</code>. When a <code>shared_ptr<void></code> instance constructed as:</p>
9899 <div class="literalblock">
9900 <div class="content">
9901 <pre>shared_ptr<void> pv(new X);</pre>
9904 <div class="paragraph">
9905 <p>is destroyed, it will correctly dispose of the <code>X</code> object by executing <code>~X</code>.</p>
9907 <div class="paragraph">
9908 <p>This propery can be used in much the same manner as a raw <code>void*</code> is used to temporarily strip type information from an object pointer.
9909 A <code>shared_ptr<void></code> can later be cast back to the correct type by using <code><a href="#shared_ptr_static_pointer_cast">static_pointer_cast</a></code>.</p>
9913 <h3 id="techniques_associating_arbitrary_data_with_heterogeneous_shared_ptr_instances">Associating arbitrary data with heterogeneous <code>shared_ptr</code> instances</h3>
9914 <div class="paragraph">
9915 <p><code>shared_ptr</code> and <code>weak_ptr</code> support <code>operator<</code> comparisons required by standard associative containers such as <code>std::map</code>. This can be
9916 used to non-intrusively associate arbitrary data with objects managed by <code>shared_ptr</code>:</p>
9918 <div class="listingblock">
9919 <div class="content">
9920 <pre class="highlight"><code>typedef int Data;
9922 std::map<shared_ptr<void>, Data> userData;
9923 // or std::map<weak_ptr<void>, Data> userData; to not affect the lifetime
9925 shared_ptr<X> px(new X);
9926 shared_ptr<int> pi(new int(3));
9929 userData[pi] = 91;</code></pre>
9934 <h3 id="techniques_using_shared_ptr_as_a_copyconstructible_mutex_lock">Using <code>shared_ptr</code> as a <code>CopyConstructible</code> mutex lock</h3>
9935 <div class="paragraph">
9936 <p>Sometimes it’s necessary to return a mutex lock from a function, and a noncopyable lock cannot be returned by value. It is possible to use <code>shared_ptr</code> as a mutex lock:</p>
9938 <div class="listingblock">
9939 <div class="content">
9940 <pre class="highlight"><code>class mutex
9948 shared_ptr<mutex> lock(mutex & m)
9951 return shared_ptr<mutex>(&m, mem_fn(&mutex::unlock));
9955 <div class="paragraph">
9956 <p>Better yet, the <code>shared_ptr</code> instance acting as a lock can be encapsulated in a dedicated <code>shared_lock</code> class:</p>
9958 <div class="listingblock">
9959 <div class="content">
9960 <pre class="highlight"><code>class shared_lock
9964 shared_ptr<void> pv;
9968 template<class Mutex> explicit shared_lock(Mutex & m): pv((m.lock(), &m), mem_fn(&Mutex::unlock)) {}
9972 <div class="paragraph">
9973 <p><code>shared_lock</code> can now be used as:</p>
9975 <div class="literalblock">
9976 <div class="content">
9977 <pre>shared_lock lock(m);</pre>
9980 <div class="paragraph">
9981 <p>Note that <code>shared_lock</code> is not templated on the mutex type, thanks to <code>shared_ptr<void></code>’s ability to hide type information.</p>
9985 <h3 id="techniques_using_shared_ptr_to_wrap_member_function_calls">Using shared_ptr to wrap member function calls</h3>
9986 <div class="paragraph">
9987 <p><code>shared_ptr</code> implements the ownership semantics required from the <code>Wrap/CallProxy</code> scheme described in Bjarne Stroustrup’s article
9988 "Wrapping C++ Member Function Calls" (available online at <a href="http://www.stroustrup.com/wrapper.pdf" class="bare">http://www.stroustrup.com/wrapper.pdf</a>). An implementation is given below:</p>
9990 <div class="listingblock">
9991 <div class="content">
9992 <pre class="highlight"><code>template<class T> class pointer
10000 explicit pointer(T * p): p_(p)
10004 shared_ptr<T> operator->() const
10007 return shared_ptr<T>(p_, mem_fn(&T::suffix));
10017 friend class pointer<X>;
10029 pointer<X> px(&x);
10037 <div class="sect2">
10038 <h3 id="techniques_delayed_deallocation">Delayed deallocation</h3>
10039 <div class="paragraph">
10040 <p>In some situations, a single <code>px.reset()</code> can trigger an expensive deallocation in a performance-critical region:</p>
10042 <div class="listingblock">
10043 <div class="content">
10044 <pre class="highlight"><code>class X; // ~X is expensive
10048 shared_ptr<X> px;
10059 <div class="paragraph">
10060 <p>The solution is to postpone the potential deallocation by moving <code>px</code> to a dedicated free list that can be periodically emptied when performance and response times are not an issue:</p>
10062 <div class="listingblock">
10063 <div class="content">
10064 <pre class="highlight"><code>vector< shared_ptr<void> > free_list;
10068 shared_ptr<X> px;
10074 free_list.push_back(px);
10079 // periodically invoke free_list.clear() when convenient</code></pre>
10082 <div class="paragraph">
10083 <p>Another variation is to move the free list logic to the construction point by using a delayed deleter:</p>
10085 <div class="listingblock">
10086 <div class="content">
10087 <pre class="highlight"><code>struct delayed_deleter
10089 template<class T> void operator()(T * p)
10093 shared_ptr<void> pv(p);
10094 free_list.push_back(pv);
10104 <div class="sect2">
10105 <h3 id="techniques_weak_without_shared">Weak pointers to objects not managed by a shared_ptr</h3>
10106 <div class="paragraph">
10107 <p>Make the object hold a <code>shared_ptr</code> to itself, using a <code>null_deleter</code>:</p>
10109 <div class="listingblock">
10110 <div class="content">
10111 <pre class="highlight"><code>class X
10115 shared_ptr<X> this_;
10120 explicit X(int i): this_(this, null_deleter()), i_(i)
10124 // repeat in all constructors (including the copy constructor!)
10126 X(X const & rhs): this_(this, null_deleter()), i_(rhs.i_)
10130 // do not forget to not assign this_ in the copy assignment
10132 X & operator=(X const & rhs)
10137 weak_ptr<X> get_weak_ptr() const { return this_; }
10141 <div class="paragraph">
10142 <p>When the object’s lifetime ends, <code>X::this_</code> will be destroyed, and all weak pointers will automatically expire.</p>
10147 <div class="sect1">
10148 <h2 id="history">Appendix B: History and Acknowledgments</h2>
10149 <div class="sectionbody">
10150 <div class="sect2">
10151 <h3 id="history_summer_1994">Summer 1994</h3>
10152 <div class="paragraph">
10153 <p>Greg Colvin <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1994/N0555.pdf">proposed</a>
10154 to the C++ Standards Committee classes named <code>auto_ptr</code> and <code>counted_ptr</code> which were very
10155 similar to what we now call <code>scoped_ptr</code> and <code>shared_ptr</code>. In one of the very few cases
10156 where the Library Working Group’s recommendations were not followed by the full committee,
10157 <code>counted_ptr</code> was rejected and surprising transfer-of-ownership semantics were added to <code>auto_ptr</code>.</p>
10160 <div class="sect2">
10161 <h3 id="history_october_1998">October 1998</h3>
10162 <div class="paragraph">
10163 <p>Beman Dawes proposed reviving the original semantics under the names <code>safe_ptr</code> and <code>counted_ptr</code>,
10164 meeting of Per Andersson, Matt Austern, Greg Colvin, Sean Corfield, Pete Becker, Nico Josuttis,
10165 Dietmar KĆ¼hl, Nathan Myers, Chichiang Wan and Judy Ward. During the discussion, the four new class
10166 names were finalized, it was decided that there was no need to exactly follow the <code>std::auto_ptr</code>
10167 interface, and various function signatures and semantics were finalized.</p>
10169 <div class="paragraph">
10170 <p>Over the next three months, several implementations were considered for <code>shared_ptr</code>, and discussed
10171 on the <a href="http://www.boost.org/">boost.org</a> mailing list. The implementation questions revolved around
10172 the reference count which must be kept, either attached to the pointed to object, or detached elsewhere.
10173 Each of those variants have themselves two major variants:</p>
10175 <div class="ulist">
10178 <p>Direct detached: the <code>shared_ptr</code> contains a pointer to the object, and a pointer to the count.</p>
10181 <p>Indirect detached: the <code>shared_ptr</code> contains a pointer to a helper object, which in turn contains a pointer to the object and the count.</p>
10184 <p>Embedded attached: the count is a member of the object pointed to.</p>
10187 <p>Placement attached: the count is attached via operator new manipulations.</p>
10191 <div class="paragraph">
10192 <p>Each implementation technique has advantages and disadvantages. We went so far as to run various timings
10193 of the direct and indirect approaches, and found that at least on Intel Pentium chips there was very little
10194 measurable difference. Kevlin Henney provided a paper he wrote on "Counted Body Techniques." Dietmar KĆ¼hl
10195 suggested an elegant partial template specialization technique to allow users to choose which implementation
10196 they preferred, and that was also experimented with.</p>
10198 <div class="paragraph">
10199 <p>But Greg Colvin and Jerry Schwarz argued that "parameterization will discourage users", and in the end we choose
10200 to supply only the direct implementation.</p>
10203 <div class="sect2">
10204 <h3 id="history_may_1999">May 1999</h3>
10205 <div class="paragraph">
10206 <p>In April and May, 1999, Valentin Bonnard and David Abrahams made a number of suggestions resulting in numerous improvements.</p>
10209 <div class="sect2">
10210 <h3 id="history_september_1999">September 1999</h3>
10211 <div class="paragraph">
10212 <p>Luis Coelho provided <code>shared_ptr::swap</code> and <code>shared_array::swap</code>.</p>
10215 <div class="sect2">
10216 <h3 id="history_november_1999">November 1999</h3>
10217 <div class="paragraph">
10218 <p>Darin Adler provided <code>operator ==</code>, <code>operator !=</code>, and <code>std::swap</code> and <code>std::less</code> specializations for shared types.</p>
10221 <div class="sect2">
10222 <h3 id="history_may_2001">May 2001</h3>
10223 <div class="paragraph">
10224 <p>Vladimir Prus suggested requiring a complete type on destruction. Refinement evolved in discussions including Dave Abrahams,
10225 Greg Colvin, Beman Dawes, Rainer Deyke, Peter Dimov, John Maddock, Vladimir Prus, Shankar Sai, and others.</p>
10228 <div class="sect2">
10229 <h3 id="history_january_2002">January 2002</h3>
10230 <div class="paragraph">
10231 <p>Peter Dimov reworked all four classes, adding features, fixing bugs, splitting them into four separate headers, and adding
10232 <code>weak_ptr</code>.</p>
10235 <div class="sect2">
10236 <h3 id="history_march_2003">March 2003</h3>
10237 <div class="paragraph">
10238 <p>Peter Dimov, Beman Dawes and Greg Colvin <a href="http://open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1450.html">proposed</a> <code>shared_ptr</code>
10239 and <code>weak_ptr</code> for inclusion in the Standard Library via the first Library Technical Report (known as TR1). The proposal was
10240 accepted and eventually went on to become a part of the C++ standard in its 2011 iteration.</p>
10243 <div class="sect2">
10244 <h3 id="history_july_2007">July 2007</h3>
10245 <div class="paragraph">
10246 <p>Peter Dimov and Beman Dawes <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2351.htm">proposed</a> a number of enhancements
10247 to <code>shared_ptr</code> as it was entering the working paper that eventually became the C++11 standard.</p>
10250 <div class="sect2">
10251 <h3 id="history_november_2012">November 2012</h3>
10252 <div class="paragraph">
10253 <p>Glen Fernandes provided implementations of <code>make_shared</code> and <code>allocate_shared</code> for arrays. They achieve a single allocation
10254 for an array that can be initialized with constructor arguments or initializer lists as well as overloads for default initialization
10255 and no value initialization.</p>
10257 <div class="paragraph">
10258 <p>Peter Dimov aided this development by extending <code>shared_ptr</code> to support arrays via the syntax <code>shared_ptr<T[]></code> and <code>shared_ptr<T[N]></code>.</p>
10261 <div class="sect2">
10262 <h3 id="history_april_2013">April 2013</h3>
10263 <div class="paragraph">
10264 <p>Peter Dimov <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3640.html">proposed</a> the extension of <code>shared_ptr</code> to support
10265 arrays for inclusion into the standard, and it was accepted.</p>
10268 <div class="sect2">
10269 <h3 id="history_february_2014">February 2014</h3>
10270 <div class="paragraph">
10271 <p>Glen Fernandes updated <code>make_shared</code> and <code>allocate_shared</code> to conform to the specification in C++ standard paper
10272 <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3870.html">N3870</a>, and implemented <code>make_unique</code> for arrays and objects.</p>
10274 <div class="paragraph">
10275 <p>Peter Dimov and Glen Fernandes updated the scalar and array implementations, respectively, to resolve C++ standard library defect 2070.</p>
10278 <div class="sect2">
10279 <h3 id="history_february_2017">February 2017</h3>
10280 <div class="paragraph">
10281 <p>Glen Fernandes rewrote <code>allocate_shared</code> and <code>make_shared</code> for arrays for a more optimal and more maintainable implementation.</p>
10284 <div class="sect2">
10285 <h3 id="history_june_2017">June 2017</h3>
10286 <div class="paragraph">
10287 <p>Peter Dimov and Glen Fernandes rewrote the documentation in Asciidoc format.</p>
10289 <div class="paragraph">
10290 <p>Peter Dimov added <code>atomic_shared_ptr</code> and <code>local_shared_ptr</code>.</p>
10293 <div class="sect2">
10294 <h3 id="history_august_2019">August 2019</h3>
10295 <div class="paragraph">
10296 <p>Glen Fernandes implemented <code>allocate_unique</code> for scalars and arrays.</p>
10301 <div class="sect1">
10302 <h2 id="shared_array">Appendix C: shared_array (deprecated)</h2>
10303 <div class="sectionbody">
10304 <div class="admonitionblock note">
10308 <div class="title">Note</div>
10310 <td class="content">
10311 This facility is deprecated because a <code>shared_ptr</code> to <code>T[]</code> or <code>T[N]</code>
10312 is now available, and is superior in every regard.
10317 <div class="sect2">
10318 <h3 id="shared_array_description">Description</h3>
10319 <div class="paragraph">
10320 <p>The <code>shared_array</code> class template stores a pointer to a dynamically allocated
10321 array. (Dynamically allocated array are allocated with the C++ <code>new[]</code>
10322 expression.) The object pointed to is guaranteed to be deleted when the last
10323 <code>shared_array</code> pointing to it is destroyed or reset.</p>
10325 <div class="paragraph">
10326 <p>Every <code>shared_array</code> meets the <em>CopyConstructible</em> and <em>Assignable</em>
10327 requirements of the C++ Standard Library, and so can be used in standard
10328 library containers. Comparison operators are supplied so that shared_array
10329 works with the standard library’s associative containers.</p>
10331 <div class="paragraph">
10332 <p>Normally, a <code>shared_array</code> cannot correctly hold a pointer to an object that
10333 has been allocated with the non-array form of <code>new</code>. See <code>shared_ptr</code> for that
10336 <div class="paragraph">
10337 <p>Because the implementation uses reference counting, cycles of <code>shared_array</code>
10338 instances will not be reclaimed. For example, if <code>main</code> holds a shared_array
10339 to <code>A</code>, which directly or indirectly holds a shared_array back to <code>A</code>, the use
10340 count of <code>A</code> will be 2. Destruction of the original <code>shared_array</code> will leave
10341 <code>A</code> dangling with a use count of 1.</p>
10343 <div class="paragraph">
10344 <p>A <code>shared_ptr</code> to a <code>std::vector</code> is an alternative to a <code>shared_array</code> that
10345 is a bit heavier duty but far more flexible.</p>
10347 <div class="paragraph">
10348 <p>The class template is parameterized on <code>T</code>, the type of the object pointed to.
10349 <code>shared_array</code> and most of its member functions place no requirements on <code>T</code>;
10350 it is allowed to be an incomplete type, or <code>void</code>. Member functions that do
10351 place additional requirements (constructors, reset) are explicitly documented
10355 <div class="sect2">
10356 <h3 id="shared_array_synopsis">Synopsis</h3>
10357 <div class="listingblock">
10358 <div class="content">
10359 <pre class="highlight"><code>namespace boost {
10361 template<class T> class shared_array {
10363 typedef T element_type;
10365 explicit shared_array(T* p = 0);
10366 template<class D> shared_array(T* p, D d);
10367 shared_array(const shared_array& v) noexcept;
10369 ~shared_array() noexcept;
10371 shared_array& operator=(const shared_array& v) noexcept;
10373 void reset(T* p = 0);
10374 template<class D> void reset(T* p, D d);
10376 T& operator[](std::ptrdiff_t n) const noexcept;
10377 T* get() const noexcept;
10379 bool unique() const noexcept;
10380 long use_count() const noexcept;
10382 explicit operator bool() const noexcept;
10384 void swap(shared_array<T>& v) noexcept;
10387 template<class T> bool
10388 operator==(const shared_array<T>& a, const shared_array<T>& b) noexcept;
10389 template<class T> bool
10390 operator!=(const shared_array<T>& a, const shared_array<T>& b) noexcept;
10391 template<class T> bool
10392 operator<(const shared_array<T>& a, const shared_array<T>& b) noexcept;
10394 template<class T>
10395 void swap(shared_array<T>& a, shared_array<T>& b) noexcept;
10400 <div class="sect2">
10401 <h3 id="shared_array_members">Members</h3>
10402 <div class="sect3">
10403 <h4 id="shared_array_element_type">element_type</h4>
10404 <div class="listingblock">
10405 <div class="content">
10406 <pre class="highlight"><code>typedef T element_type;</code></pre>
10409 <div class="dlist">
10411 <dt class="hdlist1">Type</dt>
10413 <p>Provides the type of the stored pointer.</p>
10418 <div class="sect3">
10419 <h4 id="shared_array_constructors">Constructors</h4>
10420 <div class="listingblock">
10421 <div class="content">
10422 <pre class="highlight"><code>explicit shared_array(T* p = 0);</code></pre>
10425 <div class="ulist none">
10429 <div class="dlist">
10431 <dt class="hdlist1">Effects</dt>
10433 <p>Constructs a <code>shared_array</code>, storing a copy of <code>p</code>, which must be a
10434 pointer to an array that was allocated via a C++ <code>new[]</code> expression or be 0.
10435 Afterwards, the use count is 1 (even if <code>p == 0</code>; see <code>~shared_array</code>).</p>
10437 <dt class="hdlist1">Requires</dt>
10439 <p><code>T</code> is a complete type.</p>
10441 <dt class="hdlist1">Throws</dt>
10443 <p><code>std::bad_alloc</code>. If an exception is thrown, <code>delete[] p</code> is called.</p>
10450 <div class="listingblock">
10451 <div class="content">
10452 <pre class="highlight"><code>template<class D> shared_array(T* p, D d);</code></pre>
10455 <div class="ulist none">
10459 <div class="dlist">
10461 <dt class="hdlist1">Effects</dt>
10463 <p>Constructs a <code>shared_array</code>, storing a copy of <code>p</code> and of <code>d</code>.
10464 Afterwards, the use count is 1. When the the time comes to delete the array
10465 pointed to by <code>p</code>, the object <code>d</code> is used in the statement <code>d(p)</code>.</p>
10467 <dt class="hdlist1">Requires</dt>
10472 <p><code>T</code> is a complete type.</p>
10475 <p>The copy constructor and destructor of <code>D</code> must not throw.</p>
10478 <p>Invoking the object <code>d</code> with parameter <code>p</code> must not throw.</p>
10479 <div class="dlist">
10481 <dt class="hdlist1">Throws</dt>
10483 <p><code>std::bad_alloc</code>. If an exception is thrown, <code>d(p)</code> is called.</p>
10490 <div class="listingblock">
10491 <div class="content">
10492 <pre class="highlight"><code>shared_array(const shared_array& v) noexcept;</code></pre>
10495 <div class="ulist none">
10499 <div class="dlist">
10501 <dt class="hdlist1">Effects</dt>
10503 <p>Constructs a <code>shared_array</code>, as if by storing a copy of the pointer
10504 stored in <code>v</code>. Afterwards, the use count for all copies is 1 more than the
10505 initial use count.</p>
10507 <dt class="hdlist1">Requires</dt>
10509 <p><code>T</code> is a complete type.</p>
10517 <div class="sect3">
10518 <h4 id="shared_array_destructor">Destructor</h4>
10519 <div class="listingblock">
10520 <div class="content">
10521 <pre class="highlight"><code>~shared_array() noexcept;</code></pre>
10524 <div class="ulist none">
10528 <div class="dlist">
10530 <dt class="hdlist1">Effects</dt>
10532 <p>Decrements the use count. Then, if the use count is 0, deletes the
10533 array pointed to by the stored pointer. Note that <code>delete[]</code> on a pointer with
10534 a value of 0 is harmless.</p>
10542 <div class="sect3">
10543 <h4 id="shared_array_assignment">Assignment</h4>
10544 <div class="listingblock">
10545 <div class="content">
10546 <pre class="highlight"><code>shared_array& operator=(const shared_array& v) noexcept;</code></pre>
10549 <div class="ulist none">
10553 <div class="dlist">
10555 <dt class="hdlist1">Effects</dt>
10557 <p>Constructs a new <code>shared_array</code> as described above, then replaces
10558 this <code>shared_array</code> with the new one, destroying the replaced object.</p>
10560 <dt class="hdlist1">Requires</dt>
10562 <p><code>T</code> is a complete type.</p>
10564 <dt class="hdlist1">Returns</dt>
10566 <p><code>*this</code>.</p>
10574 <div class="sect3">
10575 <h4 id="shared_array_reset">reset</h4>
10576 <div class="listingblock">
10577 <div class="content">
10578 <pre class="highlight"><code>void reset(T* p = 0);</code></pre>
10581 <div class="ulist none">
10585 <div class="dlist">
10587 <dt class="hdlist1">Effects</dt>
10589 <p>Constructs a new <code>shared_array</code> as described above, then replaces
10590 this <code>shared_array</code> with the new one, destroying the replaced object.</p>
10592 <dt class="hdlist1">Requires</dt>
10594 <p><code>T</code> is a complete type.</p>
10596 <dt class="hdlist1">Throws</dt>
10598 <p><code>std::bad_alloc</code>. If an exception is thrown, <code>delete[] p</code> is called.</p>
10605 <div class="listingblock">
10606 <div class="content">
10607 <pre class="highlight"><code>template<class D> void reset(T* p, D d);</code></pre>
10610 <div class="ulist none">
10614 <div class="dlist">
10616 <dt class="hdlist1">Effects</dt>
10618 <p>Constructs a new <code>shared_array</code> as described above, then replaces
10619 this <code>shared_array</code> with the new one, destroying the replaced object.</p>
10621 <dt class="hdlist1">Requires</dt>
10626 <p><code>T</code> is a complete type.</p>
10629 <p>The copy constructor of <code>D</code> must not throw.</p>
10630 <div class="dlist">
10632 <dt class="hdlist1">Throws</dt>
10634 <p><code>std::bad_alloc</code>. If an exception is thrown, <code>d(p)</code> is called.</p>
10642 <div class="sect3">
10643 <h4 id="shared_array_indexing">Indexing</h4>
10644 <div class="listingblock">
10645 <div class="content">
10646 <pre class="highlight"><code>T& operator[](std::ptrdiff_t n) const noexcept;</code></pre>
10649 <div class="dlist">
10651 <dt class="hdlist1">Returns</dt>
10653 <p>A reference to element <code>n</code> of the array pointed to by the stored
10654 pointer. Behavior is undefined and almost certainly undesirable if the stored
10655 pointer is 0, or if <code>n</code> is less than 0 or is greater than or equal to the
10656 number of elements in the array.</p>
10658 <dt class="hdlist1">Requires</dt>
10660 <p><code>T</code> is a complete type.</p>
10665 <div class="sect3">
10666 <h4 id="shared_array_get">get</h4>
10667 <div class="listingblock">
10668 <div class="content">
10669 <pre class="highlight"><code>T* get() const noexcept;</code></pre>
10672 <div class="ulist none">
10676 <div class="dlist">
10678 <dt class="hdlist1">Returns</dt>
10680 <p>The stored pointer.</p>
10688 <div class="sect3">
10689 <h4 id="shared_array_unique">unique</h4>
10690 <div class="listingblock">
10691 <div class="content">
10692 <pre class="highlight"><code>bool unique() const noexcept;</code></pre>
10695 <div class="ulist none">
10699 <div class="dlist">
10701 <dt class="hdlist1">Returns</dt>
10703 <p><code>true</code> if no other <code>shared_array</code> is sharing ownership of the
10704 stored pointer, <code>false</code> otherwise.</p>
10712 <div class="sect3">
10713 <h4 id="shared_array_use_count">use_count</h4>
10714 <div class="listingblock">
10715 <div class="content">
10716 <pre class="highlight"><code>long use_count() const noexcept;</code></pre>
10719 <div class="ulist none">
10723 <div class="dlist">
10725 <dt class="hdlist1">Returns</dt>
10727 <p>The number of <code>shared_array</code> objects sharing ownership of the
10728 stored pointer.</p>
10736 <div class="sect3">
10737 <h4 id="shared_array_conversions">Conversions</h4>
10738 <div class="listingblock">
10739 <div class="content">
10740 <pre class="highlight"><code>explicit operator bool() const noexcept;</code></pre>
10743 <div class="ulist none">
10747 <div class="dlist">
10749 <dt class="hdlist1">Returns</dt>
10751 <p><code>get() != 0</code>.</p>
10753 <dt class="hdlist1">Requires</dt>
10755 <p><code>T</code> is a complete type.</p>
10763 <div class="sect3">
10764 <h4 id="shared_array_swap">swap</h4>
10765 <div class="listingblock">
10766 <div class="content">
10767 <pre class="highlight"><code>void swap(shared_array<T>& b) noexcept;</code></pre>
10770 <div class="ulist none">
10774 <div class="dlist">
10776 <dt class="hdlist1">Effects</dt>
10778 <p>Exchanges the contents of the two smart pointers.</p>
10787 <div class="sect2">
10788 <h3 id="shared_array_free_functions">Free Functions</h3>
10789 <div class="sect3">
10790 <h4 id="shared_array_comparison">Comparison</h4>
10791 <div class="listingblock">
10792 <div class="content">
10793 <pre class="highlight"><code>template<class T> bool
10794 operator==(const shared_array<T>& a, const shared_array<T>& b) noexcept;</code></pre>
10797 <div class="listingblock">
10798 <div class="content">
10799 <pre class="highlight"><code>template<class T> bool
10800 operator!=(const shared_array<T>& a, const shared_array<T>& b) noexcept;</code></pre>
10803 <div class="listingblock">
10804 <div class="content">
10805 <pre class="highlight"><code>template<class T> bool
10806 operator<(const shared_array<T>& a, const shared_array<T>& b) noexcept;</code></pre>
10809 <div class="ulist none">
10813 <div class="dlist">
10815 <dt class="hdlist1">Returns</dt>
10817 <p>The result of comparing the stored pointers of the two smart
10825 <div class="admonitionblock note">
10829 <div class="title">Note</div>
10831 <td class="content">
10832 The <code>operator<</code> overload is provided to define an ordering so that
10833 <code>shared_array</code> objects can be used in associative containers such as
10834 <code>std::map</code>. The implementation uses <code>std::less<T*></code> to perform the comparison.
10835 This ensures that the comparison is handled correctly, since the standard
10836 mandates that relational operations on pointers are unspecified (5.9
10837 [expr.rel] paragraph 2) but <code>std::less</code> on pointers is well-defined (20.3.3
10838 [lib.comparisons] paragraph 8).
10844 <div class="sect3">
10845 <h4 id="shared_array_swap_2">swap</h4>
10846 <div class="listingblock">
10847 <div class="content">
10848 <pre class="highlight"><code>template<class T>
10849 void swap(shared_array<T>& a, shared_array<T>& b) noexcept;</code></pre>
10852 <div class="ulist none">
10856 <div class="dlist">
10858 <dt class="hdlist1">Returns</dt>
10860 <p><code>a.swap(b)</code>.</p>
10862 <dt class="hdlist1">Requires</dt>
10864 <p><code>T</code> is a complete type.</p>
10875 <div class="sect1">
10876 <h2 id="copyright">Appendix D: Copyright and License</h2>
10877 <div class="sectionbody">
10878 <div class="paragraph">
10879 <p>This documentation is</p>
10881 <div class="ulist">
10884 <p>Copyright 1999 Greg Colvin</p>
10887 <p>Copyright 1999 Beman Dawes</p>
10890 <p>Copyright 2002 Darin Adler</p>
10893 <p>Copyright 2003-2017 Peter Dimov</p>
10896 <p>Copyright 2005, 2006 Ion GaztaƱaga</p>
10899 <p>Copyright 2008 Frank Mori Hess</p>
10902 <p>Copyright 2012-2017 Glen Fernandes</p>
10905 <p>Copyright 2013 Andrey Semashev</p>
10909 <div class="paragraph">
10910 <p>and is distributed under the <a href="http://www.boost.org/LICENSE_1_0.txt">Boost Software License, Version 1.0</a>.</p>
10916 <div id="footer-text">
10917 Last updated 2019-12-10 00:19:52 UTC
10922 *:not(pre)>code { background: none; color: #600000; }
10923 table tr.even, table tr.alt, table tr:nth-of-type(even) { background: none; }