草庐IT

gray8_ptr_t

全部标签

c++ - shared_ptr 的静态成员函数 make_shared

使用libc++我在公共(public)部分找到了std::shared_ptr::make_shared()静态成员函数。当我已经为std::shared_ptr的特化定义了类型别名时,这非常方便:usingT=int;usingP=std::shared_ptr;autop=P::make_shared(123);//std::make_shared(123)static_assert(std::is_same::value);我担心标准合规性,因为来自可信来源的文章(1,2)没有提到std::shared_ptr的静态成员函数make_shared>.目前使用该功能是否不好?为什

c++ - 在 map 中使用 unique_ptr 时删除 std::pair 中的函数

我有一段C++代码,我不确定它是否正确。考虑以下代码。#include#include#includeusingnamespacestd;intmain(intargc,char*argv[]){vector>>v;v.resize(5);returnEXIT_SUCCESS;}GCC编译这段代码没有问题。然而,英特尔编译器(版本19)因错误而停止:/usr/local/[...]/include/c++/7.3.0/ext/new_allocator.h(136):error:function"std::pair::pair(conststd::pair&)[with_T1=cons

C++ 使用 C++11 (std::shared_ptr) 分配 shared_ptr:将 shared_ptr 初始化为临时变量是否仍然不好?

我正在阅读thisanswer作者指的是boostbestpractices其中说:Avoidusingunnamedshared_ptrtemporariestosavetyping;toseewhythisisdangerous,considerthisexample:voidf(shared_ptr,int);intg();voidok(){shared_ptrp(newint(2));f(p,g());}voidbad(){f(shared_ptr(newint(2)),g());}Thefunctionokfollowstheguidelinetotheletter,wher

c++ - Pimpl with smart ptr - 为什么需要构造函数/析构函数

这个问题在这里已经有了答案:std::unique_ptrwithanincompletetypewon'tcompile(7个答案)关闭8年前。让我们考虑以下示例(使用c++11)A.hpp:#includeclassA{public://A();//~A();private:structAImpl;std::unique_ptrpImpl;};主要.cpp:#include"A.hpp"intmain(){Aa;}使用默认构造函数和析构函数。不编译。发生以下错误:Infileincludedfrom/usr/include/c++/4.8/memory:81:0,fromA.hpp

c++ - 图节点父列表中的 weak_ptr VS shared_ptr

我有一个由Graph和Node类实现的有向无环图。每个节点都有一个指向子节点的指针列表和一个指向父节点的指针列表。我最近添加了父级,因为一些算法需要快速访问父级列表,而且图很小,每个节点只有很少的连接,所以没有内存问题。子列表使用std::shared_ptr以便节点至少在它们有父节点时保留在内存中。但是我不希望节点拥有它的父节点,所以我使用weak_ptr作为指向父节点的指针。但是后来算法出了问题。算法必须从weak_ptr创建一个新的shared_ptr,所以我不能直接使用operator==,并且使用标准函数如std::find()需要编写一个调用my_weak_ptr.lock

c++ - 什么是auto_ptr_ref,它实现了什么以及如何实现的

auto_ptr_ref文档here说这个这是一个工具类,允许进行某些转换,这些转换允许将auto_ptr对象传递给函数并从函数返回。有人可以解释一下auto_ptr_ref如何帮助实现这一目标。我只想了解auto_ptr类及其内部结构 最佳答案 这有点令人困惑。基本上,auto_ptr_ref存在是因为auto_ptr拷贝构造函数并不是标准意义上的拷贝构造函数。复制构造函数通常有一个如下所示的签名:X(constX&b);auto_ptr复制构造函数的签名如下所示:X(X&b)这是因为auto_ptr需要修改从中复制的对象,以便将

c++ - 使用 std::dynamic_pointer_cast 向上转换 std::shared_ptr

我开始在C++0X/11中使用智能指针,但遇到了一个特殊情况。我想使用shared_ptr向上转换一个对象的实例。Extend类继承自Base类,其中Base类具有虚拟析构函数以使其具有多态性(否则dynamic_pointer_cast会提示非多态类转换)。如果因此:std::shared_ptrobj=std::make_shared();然后我做:obj=std::dynamic_pointer_cast(obj);安全吗?其他指向该对象的指针会怎样?是否只有obj将其视为Extend,而其他共享指针仍将其视为Base?向上转换同一实例是否安全,还是我应该做其他事情?编辑:感谢您

c++ - std::shared_ptr - 将共享指针作为参数传递的最佳实践

我已经离开严肃的C++大约十年了。我又回来了,目前正在从事一个项目,以完全熟悉C++11。关于如何最好地传递std::shared_ptr,我遇到了一些生存危机。举个简单的例子,采用以下设置:classServiceB{public:ServiceB(){}};classServiceA{public:ServiceA(std::shared_ptr&serviceB):_serviceB(serviceB){}private:std::shared_ptr_serviceB;};classRoot{public:Root():_serviceB(std::shared_ptr(new

c++ - 将 shared_ptr 分配给数组的偏移量

假设我有一个数组的shared_ptr:std::shared_ptrsp(newT[10],[](T*p){delete[]p;});还有一个方法:shared_ptrptr_at_offset(intoffset){//Iwanttoreturnashared_ptrto(sp.get()+offset)here//inawaythatthereferencecounttospisincremented...}基本上,我想做的是返回一个新的shared_ptr来增加引用计数,但指向原始数组的偏移量;我想避免在调用者以某个偏移量使用数组时删除数组。如果我只是返回sp.get()+of

c++ - `unique_ptr` 上的原子操作

std::shared_ptrhasspecializationsforatomicoperations像atomic_compare_exchange_weak和family,但我找不到关于std::unique_ptr的等效特化的文档。有没有?如果不是,为什么不呢? 最佳答案 可以提供std::shared_ptr的原子实例的原因并且不可能为std::unique_ptr这样做在他们的签名中暗示。比较:std::shared_ptr对比std::unique_ptr其中D是删除器的类型。std::shared_ptr需要分配一个