草庐IT

intrusive_ptr

全部标签

c++ - 你如何让 std::shared_ptr 不调用 delete()

我有将std::shared_ptr作为参数的函数,所以我被迫使用std::shared_ptr,但我传递给函数的对象不是动态分配的。如何将对象包装在std::shared_ptr中并让std::shared_ptr不对它调用delete。 最佳答案 MyTypet;nasty_function(std::shared_ptr(&t,[](MyType*){})); 关于c++-你如何让std::shared_ptr不调用delete(),我们在StackOverflow上找到一个类似的

c++ - 这篇关于shared_ptr的use_count()的Standardese是什么意思?

在试图解决thisquestion中显示的问题时我发现自己陷入了[util.smartptr.shared]/4中的以下句子:[...]Changesinuse_count()donotreflectmodificationsthatcanintroducedataraces.我不明白我应该怎么读,我会得出什么结论。以下是一些解释:调用use_count()不会引入数据竞争(但这应该由该函数的const-ness以及相应的库范围保证来保证)use_count()返回的值不受(“不反射(reflect)”?)需要原子性或同步的操作结果的影响(但这些相关操作是什么?)use_count()

c++ - 这篇关于shared_ptr的use_count()的Standardese是什么意思?

在试图解决thisquestion中显示的问题时我发现自己陷入了[util.smartptr.shared]/4中的以下句子:[...]Changesinuse_count()donotreflectmodificationsthatcanintroducedataraces.我不明白我应该怎么读,我会得出什么结论。以下是一些解释:调用use_count()不会引入数据竞争(但这应该由该函数的const-ness以及相应的库范围保证来保证)use_count()返回的值不受(“不反射(reflect)”?)需要原子性或同步的操作结果的影响(但这些相关操作是什么?)use_count()

c++ - 标准 <memory> 文件中 boost::shared_ptr 和 std::shared_ptr 之间的区别

我想知道boost::shared_ptr之间是否有任何区别和std::shared_ptr在标准中找到文件。 最佳答案 std::shared_ptr是tr1::shared_ptr的C++0x形式,boost的boost::shared_ptr应该表现一样。然而,std::shared_ptr,在符合C++0x标准的实现中,应该/可能对shared_ptr类有更方便的行为,如所述在以下链接中:Differencesbetweendifferentflavoursofshared_ptrhttp://en.wikipedia.or

c++ - 标准 <memory> 文件中 boost::shared_ptr 和 std::shared_ptr 之间的区别

我想知道boost::shared_ptr之间是否有任何区别和std::shared_ptr在标准中找到文件。 最佳答案 std::shared_ptr是tr1::shared_ptr的C++0x形式,boost的boost::shared_ptr应该表现一样。然而,std::shared_ptr,在符合C++0x标准的实现中,应该/可能对shared_ptr类有更方便的行为,如所述在以下链接中:Differencesbetweendifferentflavoursofshared_ptrhttp://en.wikipedia.or

c++ - 在 map 中使用自定义删除器存储 unique_ptr

为什么这不起作用?#include#includevoiddeleter(int*i){deletei;}std::map>m;voidfoo(int*i){m[0]=std::unique_ptr(i,&deleter);}查看无法理解的编译错误https://godbolt.org/z/Uhp9NO.Infileincludedfrom:1:Infileincludedfrom/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/map:61:Inf

c++ - 在 map 中使用自定义删除器存储 unique_ptr

为什么这不起作用?#include#includevoiddeleter(int*i){deletei;}std::map>m;voidfoo(int*i){m[0]=std::unique_ptr(i,&deleter);}查看无法理解的编译错误https://godbolt.org/z/Uhp9NO.Infileincludedfrom:1:Infileincludedfrom/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/map:61:Inf

c++ - 如果 unique_ptr 需要存储删除器,它怎么能没有开销?

首先看看C++Primer说的unique_ptr和shared_ptr:16.1.6美元。效率和灵activeWecanbecertainthatshared_ptrdoesnotholdthedeleterasadirectmember,becausethetypeofthedeleterisn’tknownuntilruntime.Becausethetypeofthedeleterispartofthetypeofaunique_ptr,thetypeofthedeletermemberisknownatcompiletime.Thedeletercanbestoreddire

c++ - 如果 unique_ptr 需要存储删除器,它怎么能没有开销?

首先看看C++Primer说的unique_ptr和shared_ptr:16.1.6美元。效率和灵activeWecanbecertainthatshared_ptrdoesnotholdthedeleterasadirectmember,becausethetypeofthedeleterisn’tknownuntilruntime.Becausethetypeofthedeleterispartofthetypeofaunique_ptr,thetypeofthedeletermemberisknownatcompiletime.Thedeletercanbestoreddire

c++ - weak_ptr 的性能损失是多少?

我目前正在为游戏设计一个对象结构,在我的例子中,最自然的组织变成了一棵树。作为智能指针的忠实粉丝,我只使用shared_ptr的。然而,在这种情况下,树中的子节点需要访问它的父节点(例如——map上的生物需要能够访问map数据——因此他们的父节点的数据。拥有的方向当然是map拥有它的存在,因此持有指向它们的共享指针。然而,为了从一个存在中访问map数据,我们需要一个指向父级的指针——智能指针的方式是使用一个引用,即weak_ptr。但是,我曾经读到锁定weak_ptr是一项昂贵的操作——也许这不再是真的了——但考虑到weak_ptr会经常被锁定,我担心这种设计注定性能不佳。因此问题:锁