std::auto_ptr to std::unique_ptr

后端 未结 4 1420
無奈伤痛
無奈伤痛 2020-11-28 00:46

With the new standard coming (and parts already available in some compilers), the new type std::unique_ptr is supposed to be a replacement for std::auto_p

4条回答
  •  暖寄归人
    2020-11-28 01:28

    AFAIK, unique_ptr is not a direct replacement. The major flaw that it fixes is the implicit transfer of ownership.

    std::auto_ptr a(new int(10)), b;
    b = a; //implicitly transfers ownership
    
    std::unique_ptr a(new int(10)), b;
    b = std::move(a); //ownership must be transferred explicitly
    

    On the other hand, unique_ptr will have completely new capabilities: they can be stored in containers.

提交回复
热议问题