Idiomatic use of std::auto_ptr or only use shared_ptr?

前端 未结 5 1529
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 03:12

Now that shared_ptr is in tr1, what do you think should happen to the use of std::auto_ptr? They both have different use cases, but all use cases of

5条回答
  •  我寻月下人不归
    2021-02-04 04:04

    auto_ptr is nice in signatures, too. When a function takes an auto_ptr by value, it means it will consume the T. If a function returns an auto_ptr, it's clear that it relinquishes ownership. This can communicate your intents about the lifetime.

    On the other hand, using scoped_ptr implies that you don't want to care about the lifetime of the T. This also implies you can use it in more places. Both smart pointers are valid choices, you can certainly have both in a single program.

提交回复
热议问题