shared_ptr<>是到weak_ptr<> as unique_ptr<>是...什么? [英] shared_ptr<> is to weak_ptr<> as unique_ptr<> is to... what?

查看:106
本文介绍了shared_ptr<>是到weak_ptr<> as unique_ptr<>是...什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11中,您可以使用 shared_ptr<> 建立与对象或变量和 weak_ptr< ;

In C++11, you can use a shared_ptr<> to establish an ownership relation with an object or variable and weak_ptr<> to safely reference that object in a non-owned way.

您也可以使用 unique_ptr<> / code>与对象或变量建立所有权关系。但是如果其他的,非拥有的对象想要引用该对象呢? weak_ptr<> 在此情况下无效。原始指针是有用的,但带来了各种缺点(例如,他们可以自动初始化为nullptr ,但这是通过与 std :: * _ ptr<> 类型不一致的技术完成的。

You can also use unique_ptr<> to establish an ownership relation with an object or variable. But what if other, non-owning objects want to also reference that object? weak_ptr<> isn't helpful in this case. Raw pointers are helpful but bring various downsides (e.g. they can be automatically initialized to nullptr but this is accomplished through techniques that are not consistent with the std::*_ptr<> types).

对于通过 unique_ptr<>拥有的对象的非拥有引用,等价于 weak_ptr<> ;

What is the equivalent of weak_ptr<> for non-owning references to objects owned via unique_ptr<>?

这是一个澄清的例子,类似于我正在做的一个游戏。

Here's a clarifying example that resembles something in a game I'm working on.

class World
{
public:

    Trebuchet* trebuchet() const { return m_trebuchet.get(); }

private:
    std::unique_ptr< Trebuchet > m_trebuchet;
};

class Victim
{
public:
    Victim( Trebuchet* theTrebuchet ) : m_trebuchet( theTrebuchet ) {}

    ~Victim()
    {
        delete m_trebuchet;     // Duh. Oops. Dumb error. Nice if the compiler helped prevent this.
    }

private:

    Trebuchet* m_trebuchet;    // Non-owning.
};

shared_ptr< Victim > createVictim( World& world )
{
    return make_shared< Victim >( world.trebuchet() );
}

这里我们使用一个原始指针来维护与一个对象的非拥有关系通过 unique_ptr<> 在其他地方拥有。

Here we use a raw pointer to maintain a non-owning relationship with an object owned via unique_ptr<> elsewhere. But is raw the best we can do?

希望是一种类型的指针:

The hope is a type of pointer that:


  • 看起来像其他现代指针类型。例如。 std :: raw_ptr< T>

  • 替换原始指针,以便使用现代指针类型的代码库,

  • 自动初始化为nullptr。
  • 通过搜索 _ptr<
  • Looks like the other modern pointer types. E.g. std::raw_ptr<T>.
  • Replaces raw pointers so that a codebase that uses modern pointer types throughout can find all pointers via a search for _ptr< (roughly).
  • Auto-initializes to nullptr.

因此:

int* p;                  // Unknown value.
std::raw_ptr< int > p;   // null.

这种类型现在已经存在于C ++中,是未来提出的,可用于例如Boost?

Does this type already exist in C++ now, is it proposed for the future, or is another implementation broadly available in e.g. Boost?

推荐答案

真正需要一个标准的指针类型作为一个非拥有的,与 std :: unique_ptr<> 的行为对应。没有这样的指针已经标准化,但是已经提出了标准<并且正在由C ++标准委员会讨论。 World's Dumbest Smart Pointer,aka std :: exempt_ptr<> 将具有其他现代C ++指针类的一般语义,但是不拥有-to对象(如 shared_ptr unique_ptr )或正确响应对象的删除c $ c> weak_ptr )。

There is a genuine need for a standard pointer type to act as a non-owning, inexpensive, and well-behaved counterpoint to std::unique_ptr<>. No such pointer has been standardized yet, but a standard has been proposed and is under discussion by the C++ standards committee. The "World's Dumbest Smart Pointer", aka std::exempt_ptr<> would have the general semantics of other modern C++ pointer classes but would hold no responsibility either for owning the pointed-to object (as shared_ptr and unique_ptr do) or for correctly responding to the deletion of that object (as weak_ptr does).

假设此功能最终获得委员会的批准,题。即使委员会没有批准,上述链接文件也完全表达了需求,并描述了一个完整的解决方案。

Assuming that this feature is ultimately ratified by the committee, it would fully meet the need highlighted in this question. Even if it isn't ratified by the committee, the above linked document fully expresses the need and describes a complete solution.

这篇关于shared_ptr&lt;&gt;是到weak_ptr&lt;&gt; as unique_ptr&lt;&gt;是...什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆