std::unique_ptr 谁借莪1个温暖的怀抱¢ 2022-06-10 10:17 102阅读 0赞 # std::unique\_ptr # <table style="font-size:12.8px; background-color:transparent; display:block; padding:0.5em 0px; border-spacing:0px"> <tbody> <tr> <td style="padding:0px; font-size:0.8em; line-height:1em"> <div style="margin-left:2em"> 定义于头文件 <code style="font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace!important; background-color:transparent"><a href="http://zh.cppreference.com/mwiki/index.php?title=cpp/header/memory&action=edit&redlink=1" title="cpp/header/memory(页面不存在)" style="text-decoration:none; color:rgb(165,88,88); background:none" rel="nofollow"><memory></a></code> </div> </td> <td style="padding:0px; font-size:0.8em; line-height:1em"> </td> <td style="padding:0px; font-size:0.8em; line-height:1em"> </td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); padding:0.3em 2em 0.2em 1em; font-size:1em"> <span style="font-family:monospace; line-height:1.1em; white-space:nowrap; margin:0px; padding:0px"><span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,0,221)">template</span><span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,0,128)"><</span><br> </span> <p style="margin-top:0px; margin-bottom:0px; line-height:1.1em; padding-top:0px; padding-bottom:0px"> <span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,0,221)">class</span> T,<br> <span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,0,221)">class</span> Deleter <span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,0,128)">=</span> <a href="http://zh.cppreference.com/w/cpp/memory/default_delete" style="text-decoration:none; color:rgb(0,48,128); background:none" rel="nofollow"><span style="line-height:1.1em; margin:0px; padding:0px">std::<span style="line-height:1.1em; margin:0px; padding:0px">default_delete</span></span></a><span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,0,128)"><</span>T<span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,0,128)">></span><br> </p> <span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,0,128)">></span> <span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,0,221)">class</span> unique_ptr<span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,128,128)">;</span></td> <td style="border-top:1px solid rgb(204,204,204); font-size:0.8em; padding-right:1.5em"> (1)</td> <td style="border-top:1px solid rgb(204,204,204); padding:0px"><span style="color:rgb(0,128,0); font-size:0.8em">(C++11 起)</span></td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); padding:0.3em 2em 0.2em 1em; font-size:1em"> <span style="font-family:monospace; line-height:1.1em; white-space:nowrap; margin:0px; padding:0px"><span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,0,221)">template</span> <span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,0,128)"><</span><br> </span> <p style="margin-top:0px; margin-bottom:0px; line-height:1.1em; padding-top:0px; padding-bottom:0px"> <span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,0,221)">class</span> T,<br> <span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,0,221)">class</span> Deleter<br> </p> <span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,0,128)">></span> <span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,0,221)">class</span> unique_ptr<span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,0,128)"><</span>T<span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,128,0)">[</span><span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,128,0)">]</span>,Deleter<span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,0,128)">></span><span style="line-height:1.1em; margin:0px; padding:0px; color:rgb(0,128,128)">;</span></td> <td style="border-top:1px solid rgb(204,204,204); font-size:0.8em; padding-right:1.5em"> (2)</td> <td style="border-top:1px solid rgb(204,204,204); padding:0px"><span style="color:rgb(0,128,0); font-size:0.8em">(C++11 起)</span></td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); padding:0px"> </td> <td style="border-top:1px solid rgb(204,204,204); padding:0px"> </td> <td style="border-top:1px solid rgb(204,204,204); padding:0px"> </td> </tr> </tbody> </table> `std::unique_ptr`是具有以下特性的智能指针: * 通过指针保留了唯一的对象的所有权,并且 * `unique_ptr`离开作用域时,会析构指向的对象。 `unique_ptr`不能复制或者复制赋值,两个`unique_ptr`实例不能管理同一个对象。一个非const的`unique_ptr`可以将所管理对象的所有权转移到另一个`unique_ptr`。一个const std::unique\_ptr不能转让,而是将所管理对象的生命周期限制在指针所创建的作用域之中。当`unique_ptr`销毁时,会通过`Deleter`销毁所管理的对象 有两个版本的`std::unique_ptr` 1) 管理单一的对象的生命周期,例如使用 new 来分配 2) 管理一个数组的生命周期与运行时间长度,例如使用 new\[\] 分配 `std::unique_ptr`的典型用途包括 * 提供异常安全的类和函数来处理具有动态生命周期的对象,通过确保正常和异常情况下退出时的删除操作。 * 通过函数传递独有的动态生命周期对象的所有权 * 从函数获得独有的动态生命周期对象的所有权 * 作为移动感知的容器,类似于std::vector持有动态分配的对象的指针,例如如果需要多态行为 ### 成员类型 ### <table style="font-size:12.8px; background-color:transparent; max-width:60em; border-spacing:0px"> <tbody> <tr> <td colspan="2"> </td> </tr> <tr> <td style="font-weight:bold; line-height:1.1em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap; border-top:1px solid rgb(204,204,204)"> 成员类型</td> <td style="font-weight:bold; line-height:1.1em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap; border-top:1px solid rgb(204,204,204)"> 定义</td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); width:102.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <span style="border:1px solid rgb(214,214,214); margin:0px 2px; padding:0px 2px; display:inline-block"><span style="font-family:monospace; line-height:normal">pointer</span></span></td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> <span style="border:1px solid rgb(214,214,214); margin:0px 2px; padding:0px 2px; display:inline-block"><span style="font-family:monospace; line-height:normal; white-space:nowrap"><a href="http://zh.cppreference.com/w/cpp/types/remove_reference" style="text-decoration:none; color:rgb(0,48,128); background:none" rel="nofollow"><span>std::<span>remove_reference</span></span></a><span style="color:rgb(0,0,128)"><</span>D<span style="color:rgb(0,0,128)">></span><span style="color:rgb(0,128,128)">::</span><span>type</span><span style="color:rgb(0,128,128)">::</span><span>pointer</span></span></span>如果存在该类型的,否则T *</td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); width:102.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <span style="border:1px solid rgb(214,214,214); margin:0px 2px; padding:0px 2px; display:inline-block"><span style="font-family:monospace; line-height:normal">element_type</span></span></td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> <code style="font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace!important; background-color:transparent">T</code>,该对象的类型的此<code style="font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace!important; background-color:transparent">unique_ptr</code>管理</td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); width:102.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <span style="border:1px solid rgb(214,214,214); margin:0px 2px; padding:0px 2px; display:inline-block"><span style="font-family:monospace; line-height:normal">deleter_type</span></span></td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> <code style="font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace!important; background-color:transparent">Deleter</code>,函数对象或左值引用函数或函数对象,被调用的析构函数</td> </tr> </tbody> </table> ### 成员函数 ### <table style="font-size:12.8px; background-color:transparent; max-width:60em; border-spacing:0px"> <tbody> <tr> <td style="border-top:1px solid rgb(204,204,204); width:100.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <div> <div style="padding:0px; display:inline-table; vertical-align:middle; font-family:DejaVuSans,"DejaVu Sans",arial,sans-serif"> <a href="http://zh.cppreference.com/w/cpp/memory/unique_ptr/unique_ptr" title="cpp/memory/unique ptr/unique ptr" style="text-decoration:none; color:rgb(11,0,128); background:none" rel="nofollow">(构造函数)</a> </div> </div> </td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> 构造新的<code style="font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace!important; background-color:transparent">unique_ptr</code> <br> <span style="color:rgb(0,128,0); font-size:0.8em">(公开成员函数)</span><span title="编辑此模板" style="float:right; display:inline; margin-left:5px; font-size:0.7em"><a href="http://zh.cppreference.com/mwiki/index.php?title=Template:cpp/memory/unique_ptr/dsc_constructor&action=edit" style="text-decoration:none; color:rgb(102,51,102); padding-top:0px!important; padding-right:13px; padding-bottom:0px!important; padding-left:0px!important" rel="nofollow">[编辑]</a></span></td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); width:100.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <div> <div style="padding:0px; display:inline-table; vertical-align:middle; font-family:DejaVuSans,"DejaVu Sans",arial,sans-serif"> <a href="http://zh.cppreference.com/w/cpp/memory/unique_ptr/~unique_ptr" title="cpp/memory/unique ptr/~unique ptr" style="text-decoration:none; color:rgb(11,0,128); background:none" rel="nofollow">(析构函数)</a> </div> </div> </td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> 析构所管理的对象,如果存在的话 <br> <span style="color:rgb(0,128,0); font-size:0.8em">(公开成员函数)</span><span title="编辑此模板" style="float:right; display:inline; margin-left:5px; font-size:0.7em"><a href="http://zh.cppreference.com/mwiki/index.php?title=Template:cpp/memory/unique_ptr/dsc_destructor&action=edit" style="text-decoration:none; color:rgb(102,51,102); padding-top:0px!important; padding-right:13px; padding-bottom:0px!important; padding-left:0px!important" rel="nofollow">[编辑]</a></span></td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); width:100.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <div> <div style="padding:0px; display:inline-table; vertical-align:middle; font-weight:bold; font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace"> <a href="http://zh.cppreference.com/w/cpp/memory/unique_ptr/operator%3D" title="cpp/memory/unique ptr/operator=" style="text-decoration:none; color:rgb(11,0,128); background:none" rel="nofollow">operator=</a> </div> </div> </td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> 为<code style="font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace!important; background-color:transparent">unique_ptr</code>赋值 <br> <span style="color:rgb(0,128,0); font-size:0.8em">(公开成员函数)</span><span title="编辑此模板" style="float:right; display:inline; margin-left:5px; font-size:0.7em"><a href="http://zh.cppreference.com/mwiki/index.php?title=Template:cpp/memory/unique_ptr/dsc_operator%3D&action=edit" style="text-decoration:none; color:rgb(102,51,102); padding-top:0px!important; padding-right:13px; padding-bottom:0px!important; padding-left:0px!important" rel="nofollow">[编辑]</a></span></td> </tr> <tr> <td colspan="2"> <h5 style="background:none; margin:0px 0px 0.3em; overflow:hidden; padding-top:0.5em; padding-bottom:0.17em; border-bottom:none; width:auto; font-size:12.8px"> <span>修饰符</span></h5> </td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); width:100.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <div> <div style="padding:0px; display:inline-table; vertical-align:middle; font-weight:bold; font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace"> <a href="http://zh.cppreference.com/w/cpp/memory/unique_ptr/release" title="cpp/memory/unique ptr/release" style="text-decoration:none; color:rgb(11,0,128); background:none" rel="nofollow">release</a> </div> </div> </td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> 返回一个指向被管理对象的指针,并释放所有权 <br> <span style="color:rgb(0,128,0); font-size:0.8em">(公开成员函数)</span><span title="编辑此模板" style="float:right; display:inline; margin-left:5px; font-size:0.7em"><a href="http://zh.cppreference.com/mwiki/index.php?title=Template:cpp/memory/unique_ptr/dsc_release&action=edit" style="text-decoration:none; color:rgb(102,51,102); padding-top:0px!important; padding-right:13px; padding-bottom:0px!important; padding-left:0px!important" rel="nofollow">[编辑]</a></span></td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); width:100.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <div> <div style="padding:0px; display:inline-table; vertical-align:middle; font-weight:bold; font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace"> <a href="http://zh.cppreference.com/w/cpp/memory/unique_ptr/reset" title="cpp/memory/unique ptr/reset" style="text-decoration:none; color:rgb(11,0,128); background:none" rel="nofollow">reset</a> </div> </div> </td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> 替换所管理的对象 <br> <span style="color:rgb(0,128,0); font-size:0.8em">(公开成员函数)</span><span title="编辑此模板" style="float:right; display:inline; margin-left:5px; font-size:0.7em"><a href="http://zh.cppreference.com/mwiki/index.php?title=Template:cpp/memory/unique_ptr/dsc_reset&action=edit" style="text-decoration:none; color:rgb(102,51,102); padding-top:0px!important; padding-right:13px; padding-bottom:0px!important; padding-left:0px!important" rel="nofollow">[编辑]</a></span></td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); width:100.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <div> <div style="padding:0px; display:inline-table; vertical-align:middle; font-weight:bold; font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace"> <a href="http://zh.cppreference.com/w/cpp/memory/unique_ptr/swap" title="cpp/memory/unique ptr/swap" style="text-decoration:none; color:rgb(11,0,128); background:none" rel="nofollow">swap</a> </div> </div> </td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> 交换所管理的对象 <br> <span style="color:rgb(0,128,0); font-size:0.8em">(公开成员函数)</span><span title="编辑此模板" style="float:right; display:inline; margin-left:5px; font-size:0.7em"><a href="http://zh.cppreference.com/mwiki/index.php?title=Template:cpp/memory/unique_ptr/dsc_swap&action=edit" style="text-decoration:none; color:rgb(102,51,102); padding-top:0px!important; padding-right:13px; padding-bottom:0px!important; padding-left:0px!important" rel="nofollow">[编辑]</a></span></td> </tr> <tr> <td colspan="2"> <h5 style="background:none; margin:0px 0px 0.3em; overflow:hidden; padding-top:0.5em; padding-bottom:0.17em; border-bottom:none; width:auto; font-size:12.8px"> <span>观察器</span></h5> </td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); width:100.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <div> <div style="padding:0px; display:inline-table; vertical-align:middle; font-weight:bold; font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace"> <a href="http://zh.cppreference.com/w/cpp/memory/unique_ptr/get" title="cpp/memory/unique ptr/get" style="text-decoration:none; color:rgb(11,0,128); background:none" rel="nofollow">get</a> </div> </div> </td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> 返回指向被管理对象的指针 <br> <span style="color:rgb(0,128,0); font-size:0.8em">(公开成员函数)</span><span title="编辑此模板" style="float:right; display:inline; margin-left:5px; font-size:0.7em"><a href="http://zh.cppreference.com/mwiki/index.php?title=Template:cpp/memory/unique_ptr/dsc_get&action=edit" style="text-decoration:none; color:rgb(102,51,102); padding-top:0px!important; padding-right:13px; padding-bottom:0px!important; padding-left:0px!important" rel="nofollow">[编辑]</a></span></td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); width:100.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <div> <div style="padding:0px; display:inline-table; vertical-align:middle; font-weight:bold; font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace"> <a href="http://zh.cppreference.com/w/cpp/memory/unique_ptr/get_deleter" title="cpp/memory/unique ptr/get deleter" style="text-decoration:none; color:rgb(11,0,128); background:none" rel="nofollow">get_deleter</a> </div> </div> </td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> 返回删除器,用于被管理对象的析构 <br> <span style="color:rgb(0,128,0); font-size:0.8em">(公开成员函数)</span><span title="编辑此模板" style="float:right; display:inline; margin-left:5px; font-size:0.7em"><a href="http://zh.cppreference.com/mwiki/index.php?title=Template:cpp/memory/unique_ptr/dsc_get_deleter&action=edit" style="text-decoration:none; color:rgb(102,51,102); padding-top:0px!important; padding-right:13px; padding-bottom:0px!important; padding-left:0px!important" rel="nofollow">[编辑]</a></span></td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); width:100.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <div> <div style="padding:0px; display:inline-table; vertical-align:middle; font-weight:bold; font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace"> <a href="http://zh.cppreference.com/w/cpp/memory/unique_ptr/operator_bool" title="cpp/memory/unique ptr/operator bool" style="text-decoration:none; color:rgb(11,0,128); background:none" rel="nofollow">operator bool</a> </div> </div> </td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> 检查是否有关联的被管理对象 <br> <span style="color:rgb(0,128,0); font-size:0.8em">(公开成员函数)</span><span title="编辑此模板" style="float:right; display:inline; margin-left:5px; font-size:0.7em"><a href="http://zh.cppreference.com/mwiki/index.php?title=Template:cpp/memory/unique_ptr/dsc_operator_bool&action=edit" style="text-decoration:none; color:rgb(102,51,102); padding-top:0px!important; padding-right:13px; padding-bottom:0px!important; padding-left:0px!important" rel="nofollow">[编辑]</a></span></td> </tr> <tr> <td colspan="2"> <h5 style="background:none; margin:0px 0px 0.3em; overflow:hidden; padding-top:0.5em; padding-bottom:0.17em; border-bottom:none; width:auto; font-size:12.8px"> <span>单个对象版本,unique_ptr<T></span></h5> </td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); width:100.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <div> <div style="padding:0px; display:inline-table; vertical-align:middle; font-weight:bold; font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace"> <a href="http://zh.cppreference.com/w/cpp/memory/unique_ptr/operator*" title="cpp/memory/unique ptr/operator*" style="text-decoration:none; color:rgb(11,0,128); background:none" rel="nofollow">operator*<br> operator-></a> </div> </div> </td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> 对指向被管理对象的指针进行解引用 <br> <span style="color:rgb(0,128,0); font-size:0.8em">(公开成员函数)</span><span title="编辑此模板" style="float:right; display:inline; margin-left:5px; font-size:0.7em"><a href="http://zh.cppreference.com/mwiki/index.php?title=Template:cpp/memory/unique_ptr/dsc_operator*&action=edit" style="text-decoration:none; color:rgb(102,51,102); padding-top:0px!important; padding-right:13px; padding-bottom:0px!important; padding-left:0px!important" rel="nofollow">[编辑]</a></span></td> </tr> <tr> <td colspan="2"> <h5 style="background:none; margin:0px 0px 0.3em; overflow:hidden; padding-top:0.5em; padding-bottom:0.17em; border-bottom:none; width:auto; font-size:12.8px"> <span>数组版本,unique_ptr<T[]></span></h5> </td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); width:100.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <div> <div style="padding:0px; display:inline-table; vertical-align:middle; font-weight:bold; font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace"> <a href="http://zh.cppreference.com/w/cpp/memory/unique_ptr/operator_at" title="cpp/memory/unique ptr/operator at" style="text-decoration:none; color:rgb(11,0,128); background:none" rel="nofollow">operator[]</a> </div> </div> </td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> 提供对所管理数组的按索引访问 <br> <span style="color:rgb(0,128,0); font-size:0.8em">(公开成员函数)</span><span title="编辑此模板" style="float:right; display:inline; margin-left:5px; font-size:0.7em"><a href="http://zh.cppreference.com/mwiki/index.php?title=Template:cpp/memory/unique_ptr/dsc_operator_at&action=edit" style="text-decoration:none; color:rgb(102,51,102); padding-top:0px!important; padding-right:13px; padding-bottom:0px!important; padding-left:0px!important" rel="nofollow">[编辑]</a></span></td> </tr> </tbody> </table> ### 非成员函数 ### <table style="font-size:12.8px; background-color:transparent; max-width:60em; border-spacing:0px"> <tbody> <tr> <td style="border-top:1px solid rgb(204,204,204); width:248.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <div> <div style="padding:0px; display:inline-table; vertical-align:middle; font-weight:bold; font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace"> <a href="http://zh.cppreference.com/w/cpp/memory/unique_ptr/make_unique" title="cpp/memory/unique ptr/make unique" style="text-decoration:none; color:rgb(11,0,128); background:none" rel="nofollow">make_unique</a> </div> <div style="padding:0px 0px 0px 0.2em; display:inline-table; vertical-align:middle"> <span style="color:rgb(0,128,0); font-size:0.8em">(C++14)</span> </div> </div> </td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> 创建管理对象的唯一指针 <br> <span style="color:rgb(0,128,0); font-size:0.8em">(函数模板)</span><span title="编辑此模板" style="float:right; display:inline; margin-left:5px; font-size:0.7em"><a href="http://zh.cppreference.com/mwiki/index.php?title=Template:cpp/memory/unique_ptr/dsc_make_unique&action=edit" style="text-decoration:none; color:rgb(102,51,102); padding-top:0px!important; padding-right:13px; padding-bottom:0px!important; padding-left:0px!important" rel="nofollow">[编辑]</a></span></td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); width:248.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <div> <div style="padding:0px; display:inline-table; vertical-align:middle; font-weight:bold; font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace"> <a href="http://zh.cppreference.com/w/cpp/memory/unique_ptr/operator_cmp" title="cpp/memory/unique ptr/operator cmp" style="text-decoration:none; color:rgb(11,0,128); background:none" rel="nofollow">operator==<br> operator!=<br> operator<<br> operator<=<br> operator><br> operator>=</a> </div> </div> </td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> 比另一个 <code style="font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace!important; background-color:transparent">unique_ptr</code> 或 <span style="border:1px solid rgb(214,214,214); margin:0px 2px; padding:0px 2px; display:inline-block"><span style="font-family:monospace; line-height:normal; white-space:nowrap">nullptr</span></span> 进行比较 <br> <span style="color:rgb(0,128,0); font-size:0.8em">(函数模板)</span><span title="编辑此模板" style="float:right; display:inline; margin-left:5px; font-size:0.7em"><a href="http://zh.cppreference.com/mwiki/index.php?title=Template:cpp/memory/unique_ptr/dsc_operator_cmp&action=edit" style="text-decoration:none; color:rgb(102,51,102); padding-top:0px!important; padding-right:13px; padding-bottom:0px!important; padding-left:0px!important" rel="nofollow">[编辑]</a></span></td> </tr> <tr> <td style="border-top:1px solid rgb(204,204,204); width:248.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <div> <div style="padding:0px; display:inline-table; vertical-align:middle; font-weight:bold; font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace"> <a href="http://zh.cppreference.com/w/cpp/memory/unique_ptr/swap2" title="cpp/memory/unique ptr/swap2" style="text-decoration:none; color:rgb(11,0,128); background:none" rel="nofollow">std::swap<span style="font-weight:normal; font-size:0.7em; line-height:15.6px">(std::unique_ptr)</span></a> </div> <div style="padding:0px 0px 0px 0.2em; display:inline-table; vertical-align:middle"> <span style="color:rgb(0,128,0); font-size:0.8em">(C++11)</span> </div> </div> </td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> 特化 <span style="font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace!important">std::swap</span> 算法 <br> <span style="color:rgb(0,128,0); font-size:0.8em">(函数模板)</span><span title="编辑此模板" style="float:right; display:inline; margin-left:5px; font-size:0.7em"><a href="http://zh.cppreference.com/mwiki/index.php?title=Template:cpp/memory/unique_ptr/dsc_swap2&action=edit" style="text-decoration:none; color:rgb(102,51,102); padding-top:0px!important; padding-right:13px; padding-bottom:0px!important; padding-left:0px!important" rel="nofollow">[编辑]</a></span></td> </tr> </tbody> </table> ### 辅助类 ### <table style="font-size:12.8px; background-color:transparent; max-width:60em; border-spacing:0px"> <tbody> <tr> <td style="border-top:1px solid rgb(204,204,204); width:248.406px; line-height:1.2em; padding:0.2em 0px 0.25em 0.75em; white-space:nowrap"> <div> <div style="padding:0px; display:inline-table; vertical-align:middle; font-weight:bold; font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace"> <a href="http://zh.cppreference.com/w/cpp/memory/unique_ptr/hash" title="cpp/memory/unique ptr/hash" style="text-decoration:none; color:rgb(11,0,128); background:none" rel="nofollow">std::hash<span style="font-weight:normal; font-size:0.7em; line-height:15.6px"><std::unique_ptr></span></a> </div> <div style="padding:0px 0px 0px 0.2em; display:inline-table; vertical-align:middle"> <span style="color:rgb(0,128,0); font-size:0.8em">(C++11)</span> </div> </div> </td> <td style="border-top:1px solid rgb(204,204,204); line-height:1.1em; padding-left:0.75em"> 支持<span style="font-family:DejaVuSansMono,"DejaVu Sans Mono",courier,monospace!important">std::unique_ptr</span>的哈希函数 <br> <span style="color:rgb(0,128,0); font-size:0.8em">(类模板特化)</span><span title="编辑此模板" style="float:right; display:inline; margin-left:5px; font-size:0.7em"><a href="http://zh.cppreference.com/mwiki/index.php?title=Template:cpp/memory/unique_ptr/dsc_hash&action=edit" style="text-decoration:none; color:rgb(102,51,102); padding-top:0px!important; padding-right:13px; padding-bottom:0px!important; padding-left:0px!important" rel="nofollow">[编辑]</a></span></td> </tr> </tbody> </table> ### 示例 ### #include <iostream> #include <memory> struct Foo { Foo() { std::cout << "Foo::Foo\n"; } ~Foo() { std::cout << "Foo::~Foo\n"; } void bar() { std::cout << "Foo::bar\n"; } }; void f(const Foo &foo) { std::cout << "f(const Foo&)\n"; } int main() { std::unique_ptr<Foo> p1(new Foo); // p1 拥有 Foo if (p1) p1->bar(); { std::unique_ptr<Foo> p2(std::move(p1)); // 现在 p2 拥有 Foo f(*p2); p1 = std::move(p2); // 所有权还给了 p1 std::cout << "destroying p2...\n"; } if (p1) p1->bar(); // p1 离开作用域时, Foo 实例会自动销毁 } 输出: Foo::Foo Foo::bar f(const Foo&) destroying p2... Foo::bar Foo::~Foo
还没有评论,来说两句吧...