Replacing auto_ptr in VC++ 8

后端 未结 7 1481
Happy的楠姐
Happy的楠姐 2021-02-14 17:31

std::auto_ptr is broken in VC++ 8 (which is what we use at work). My main gripe with it is that it allows auto_ptr x = new T();, which of cour

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-14 17:46

    Move to boost smart pointers.

    In the meantime, you may want to extract a working auto_ptr implementation from an old / another STL, so you have working code.

    I believe that auto_ptr semantics are fundamentally broken - it saves typing, but the interface actually is not simpler: you still have to track which instance is the current owner and make sure the owner leaves last.

    unique-ptr "fixes" that, by making release not only give up ownership, but also setting the RHS to null. It is the closest replacement for auto-ptr, but with its different semantics it is not a drop-in replacement.

    There's an introductory article to boost smart pointers, by, ahem, me.

提交回复
热议问题