Real c++ in the smart pointer unique_ptr series - std:: unique_ptr structure (as far as possible to use std: in the C++14: make_unique, rather than new)

label Unique-ptrC++
846 people read comment(1) Collection report
Classification:

The retrospection of the blog's unique PTR one of the opening, is simple to talk about why you want to use the unique PTR with the traditional pointer compared, the advantages of smart pointer.

Start with construction now!

Look at these construction methods:

Default(One)
ConstexprUnique_ptr ()Noexcept;

Null pointer from (Two)
ConstexprUnique_ptr (nullptr_t)NoexceptUnique_ptr () {}

Pointer from (Three)
ExplicitUnique_ptr (P pointer)Noexcept;

Pointer from + Deleter lvalue (Four)
Unique_ptr (P pointer,
    TypenameConditional<is_reference<D>:: value, D,ConstDel D&>)Noexcept;

Pointer from + Deleter rvalue (Five)
Unique_ptr (P pointer,
    TypenameRemove_reference<D>:: del type&&)Noexcept;

(moveSix)
Unique_ptr (x unique_ptr&&)Noexcept;

Move-cast (Seven)
Template<ClassU,ClassE>
Unique_ptr (unique_ptr<U, X E>&&)Noexcept;

From moveAuto_ptr(Eight)
Template<ClassU>
Unique_ptr (Auto_ptrX <U>&&)Noexcept;

Copy (deleted!)Nine)
Unique_ptr (Const= = = unique_ptr&Delete;

The following is the way to construct unique_ptr:
First look at 123458

<iostream> #include
<memory> #include

IntMain () {
    STD: default_delete<Int> d;
    STD: unique_ptr<Int> u1;
    STD: unique_ptr<Int(> U2Nullptr);
    STD: unique_ptr<Int(> u3New Int);
    STD: unique_ptr<Int(> U4New IntD);
    STD: unique_ptr<Int(> U5New Int,STD: default_delete<Int> ());

    STD: unique_ptr<Int(> U8STD::Auto_ptr<Int> (New Int));

    STD::Cout""U1""(U1?"Null not":"Null"")'\n';
    If(U1 =!Nullptr)
{
        STD::Cout""*u1""*u1"STD: endl;
}

    STD::Cout""U2""(U2?"Null not":"Null"")'\n';
    If(U2 =!Nullptr)
{
        STD::Cout""*u2""*u2"STD: endl;
}

    STD::Cout""U3""(U3?"Null not":"Null"")'\n';
    If(U3 =!Nullptr)
{
        STD::Cout""*u3""*u3"STD: endl;
}

    STD::Cout""U4""(U4?"Null not":"Null"")'\n';
    If(U4 =!Nullptr)
{
        STD::Cout""*u4""*u4"STD: endl;
}

    STD::Cout""U5""(U5?"Null not":"Null"")'\n';
    If(U5 =!Nullptr)
{
        STD::Cout""*u5""*u5"STD: endl;
}


    STD::Cout""U8""(U8?"Null not":"Null"")'\n';
    If(U8 =!Nullptr)
{
        STD::Cout""*u8""*u8"STD: endl;
}

    Return Zero;
}
The output is as follows:
Null u1:
Null u2:
Not null u3:
- *u3:Eight hundred and forty-two million one hundred and fifty thousand four hundred and fifty-one
Not null u4:
- *u4:Eight hundred and forty-two million one hundred and fifty thousand four hundred and fifty-one
Not null u5:
- *u5:Eight hundred and forty-two million one hundred and fifty thousand four hundred and fifty-one
Not null u8:
- *u8:Eight hundred and forty-two million one hundred and fifty thousand four hundred and fifty-one

Analysis can be seen that the construction method is equivalent to the constructor two 12, which is nullptr, and the other is garbage.

Constructor 3 may be our most familiar form such as P int* = int new;

Here's the constructor 45 used a std:: default_delete, what is this ghost?
Std:: is the default_delete default destruction policy used by std:: when no Deleter is specified. unique_ptr
1) non-specialized default_delete uses delete to deallocate memory for a single object. The
2) partial specialization for array types that uses delete[] is also provided. A

Constructor 8 is constructed from other smart pointers unique_ptr.

Next, the remaining construction methods 6 and 7, are using the std:: move semantics, we add, to demonstrate:

<iostream> #include
<memory> #include

IntMain () {
    STD: default_delete<Int> d;
    STD: unique_ptr<Int> u1;
    STD: unique_ptr<Int(> U2Nullptr);
    STD: unique_ptr<Int(> u3New Int);
    STD: unique_ptr<Int(> U4New IntD);
    STD: unique_ptr<Int(> U5New Int,STD: default_delete<Int> ());

    STD: unique_ptr<Int(> U8STD::Auto_ptr<Int> (New Int));

    STD::::::cout<<“U1”<<(U1的吗?“不空”“零”<<)“\n”;;;;;;;
    如果(U1的。=nullptr)
{
        性病::::::cout<<“* U1:”* U1<<<<性病::::::<< endl;
}

    性病::::::cout<<“U2”<<(U2乐队?“不空”“零”<<)“\n”;;;;;;;
    如果(U2乐队!=nullptr)
{
        性病::::::cout<<“×2”U2<<<<*性病::::::<< endl;
}

    性病::::::cout<<“U3”<<(U3的)?“不空”“零”<<)“\n”;;;;;;;
    如果(U3的!=nullptr)
{
        性病::::::cout<<“×3”<<<<*U3性病::::::<< endl;
}

    性病::::::cout<<“U4”<<(U4)?“不空”“零”<<)“\n”;;;;;;;
    如果(U4!=nullptr)
{
        性病::::::cout<<“U4”<<<<*U4性病::::::<< endl;
}

    性病::::::cout<<“U5:”<<(U5?“不空”“零”<<)“\n”;;;;;;;
    如果(U5!=nullptr)
{
        性病::::::cout<<“U5:*”<<<<*U5性病::::::<< endl;
}


    性病::::::cout<<“U8:”<<(U8吗?“不空”“零”<<)“\n”;;;;;;;
    如果(U8!=nullptr)
{
        性病::::::cout<<“U8:*”<<<<*U8性病::::::<< endl;
}

    性病::独特的_ PTR。int>(U6性病::::::move(U5));
    性病::::::cout<<“U”<<(U6?“不空”“零”<<)“\n”;;;;;;;
    如果(U6!=nullptr)
{
        性病::::::cout<<“* U6:”<<<<*U6性病::::::<< endl;
}

    性病::::::cout<<“现在,让美国U5:湖”;;;;;;;
    性病::::::cout<<“U5:”<<(U5?“不空”“零”<<)“\n”;;;;;;;
    如果(U5!=nullptr)
{
        性病::::::cout<<“U5:*”<<<<*U5性病::::::<< endl;
}


    性病::独特的_ PTR。intU7(>性病::::::move(U6));
    性病::::::cout<<“U47”<<(U7?“不空”“零”<<)“\n”;;;;;;;
    如果(U7线。=nullptr)
{
        性病::::::cout<<“* U7:”<<<<*U7性病::::::<< endl;
}

    性病::::::cout<<“现在,让我们U6:湖”;;;;;;;
    性病::::::cout<<“U”<<(U6?“不空”“零”<<)“\n”;;;;;;;
    如果(U6!=nullptr)
{
        性病::::::cout<<“* U6:”<<<<*U6性病::::::<< endl;
}

    返回 0;;;;;;;
}
输出:/ /
//U1:零
//U2:零
//U3:null
/ / *:842150451 U3
//U4:null
*:842150451 U4号
//U5:null
/ / *:842150451 U5
//U8:null
842150451 U8:/ / *
//U6:null
/ / *:842150451 U6
现在让我们湖/,U5:U5:零
//U7:null
/ / *:842150451 U7
现在让我们湖/,U6 U6:零:

这里最最想说明的就是:
u5原来不是null但是std move后u5就变为了空::,,,
u6最开始也不为空,性病::move后,u6也变为了空。

还需要强调的一点,就是在对指针进行解除引用的时候,一定要验证指针是否为空,如果指针为空,再进行取值操作,程序就会崩溃.严重的bug.

如果我写到现在就停下来,你又会说我是标题党,但是我不是。
继续:
性病::_ unique没有纳入c化妆+ + + + 14的内容:11,是c
构造函数有三个:
模板类的类参数t <,>……
组成_独特独特的_ PTR(args &…args);

T><类模板
组成_独特独特的_ PTR(_ std::size t size);

模板类的类参数t <,>……
unspecified * / / *(&独特的化妆_ args…args)=删除;

# include <对象>
# include <存储>

结构vec3
{
    intx,y,z;
vec3(X):(0(,Y)0()Z0){ }
vec3(intXintY,intz):x(x),y(y),z(Z)}
    的朋友 性病::::::ostream &算子<<(性病::::::ostream和OS,vec3 & V){
        返回OS <<“”<<“X:”<<<<98“Y”v.y<<<<的“Z”v.z<<<<“}”;;;;;;;
}
};

intmain()
{
    //使用默认构造函数。
    性病::独特的_ PTR vec3 V1 = < >性病组成:_:独特的vec3><();
    //使用构造函数参数匹配研究。
    性病::独特的_ PTR vec3 V2 = < >性病组成:_独特:< >(vec3012);
    //创建一个独特的_ PTR阵列元素5
    性病::独特的_ PTR vec3 V3 = < > [ ]性病组成:_独特:< >(vec3 [ ]5);

    性病::::::cout<<“让_独特vec3():<>”<<<<×V1“\n”
<<“让_独特vec3(0,1,2):<>”<<<<×V2“\n”
<<“让_独特vec3><[(5)]:”<<“\n”;;;;;;;
    forinti =0;i<5;i++){
        性病::::::cout<<“”〔i〕<<<<V3“\n”;;;;;;;
}
}

输出:/ /
组成_ / < >():独特的vec3 { x 0 0 0}:y:z
组成_独特vec3 / < >(0、1、2):{(x,y,z:::0 1 2}
组成_ /独特的vec3 [ ] < >(5):
//{X Y Z:::0 0 0}
//{X Y Z:::0 0 0}
//{X Y Z:::0 0 0}
//{X Y Z:::0 0 0}
//{X Y Z:::0 0 0}

正如你看到的_ unique完美的传递了参数给对象的构造函数,化妆,从一个原始指针构造出一个std::::返回创建的std独特的,独特的_ ptr.这个形式的函数不支持数组和定制删除器

同直接使用new相比,make函数减小了代码重复提高的异常安全,化妆,并且对于std::::_ shared和std allcoated _共享,生成的代码会更小更快。

此时,可以参考博客Http://prog3.com/sbdm/blog/coolmeme/article/details/43405155

Bole comments
A total of1Bole recommended

  • Wangyaninglm  21:11:37 2016-01-07
  • That what, still do not have too much to understand than the ordinary pointer fortunately there, for analysis, benefit
  • top
    Zero
    step on
    Zero
    Guess you're looking for
    View comments
    * the above user comments only represent their personal views, does not represent the views or position of the CSDN website
      personal data
      • visit177027 times
      • Integral:Four thousand five hundred and ninety-one
      • Grade
      • Rank:3529th name
      • original240
      • Reproduced:16
      • Translation:12
      • Comments:183
      Blog column
      Classification of articles