草庐IT

unique-identifiers

全部标签

c++ - 如何通过 std::unique_ptr 成员调用其他类的 const 成员函数

在阅读有关向标准库添加const-propagating包装器的提案(文档编号N4388)时,我碰到了论文中给出的示例:#include#includestructA{voidbar()const{std::cout()){}voidfoo()const{std::coutbar();//callsA::bar()(non-const)//const_cast>(m_ptrA)->bar();//howtocalltheA::bar()const?}voidfoo(){std::coutbar();}std::unique_ptrm_ptrA;};intmain(){constBcon

c++ - 为什么 std::shared_ptr<T> = std::unique_ptr<T[]> 编译,而 std::shared_ptr<T[]> = std::unique_ptr<T[]> 不编译?

我使用以下输入命令在Coliru中探索了这个主题:g++-std=c++14-O2-Wall-pedantic-pthreadmain.cpp&&./a.out测试可以查到here,但我已经发布了下面的代码。我用了int在我的示例中,因为它是基本类型。#include#includestructFoo{Foo():a_{0},b_{1},c_{-1},combination_{0.5}{}inta_,b_,c_;doublecombination_;};intmain(){//int//*unManagedArray=newint[16];std::unique_ptruniqueAr

c++ - 添加从 unique_ptr<T> 到 T* 的隐式转换

一般问题:在不考虑这是否是一个好主意的情况下,如何将隐式转换运算符添加到已定义的类中?例如,假设我希望unique_ptr隐式转换为T*,但我不能只添加成员转换运算符,因为我不能更改unique_ptr类的定义。选项:是否有一些c++巫术可以让我在不创建成员函数的情况下实现这一点?迄今为止的答案:否。无法从无法在代码中修改的类型中添加隐式转换。只是……悲伤。我可以从std::unique_ptr派生并添加我自己的成员转换函数吗?这有什么严重的缺点吗?到目前为止的回答:是的(来自vsoftco)缺点尚未确定。到目前为止,从std::unique_ptr继承、继承其构造函数并声明隐式转换运

c++ - shared_ptr, unique_ptr, ownership,在这种特殊情况下我是不是做得太过分了?

我在图形应用程序上工作,并且一直在使用共享指针和唯一指针,主要是因为它为我处理内存重新分配(又名方便),这可能很糟糕(如果这就是我使用它们的原因)。我最近在Stackoverflow上阅读了一个问题的答案,其中提到根据B.Stroustrup的说法,通常不应使用唯一/共享指针,而应改为按值传递参数。我有一个图形方面的案例,我认为使用shared_ptr是有意义的,但我想从专家那里知道(我不是C++专家)我是否在做/想它,如果是的话他们会做什么(为了符合C++推荐和效率)我将以渲染/光线追踪中出现的一般问题为例。在这个特定的问题中,我们有一个对象池(我们将使用三角形来进行解释)和一个结构

c++ - 我们如何从成员函数返回一个 unique_pointer 成员?

我有一个带有指针成员的基类。我必须做出有根据的猜测来确定它应该是unique_ptr还是shared_ptr。它们似乎都无法解决我的特定用例。classBase{public:Base():pInt(std::unique_ptr(newint(10))){};virtualstd::unique_ptrget()=0;//Base():pInt(std::shared_ptr(newint(10))){};//Alternateimplementation//virtualstd::shared_ptrget()=0;//Alternateimplementationprivate:

c++ - RAII、unique_ptr 和输出参数

我是一名尝试学习C++11的C#开发人员。我正在尝试使用windns.h查询DNS。我从DnsQuery()开始,读到我需要使用DnsRecordListFree()释放结果记录输出参数。C#方法可能是使用try-finallyblock来确保我无论如何都释放资源。但我了解到没有finallyblock,并且windns.h确实应该与时俱进并实现RAII兼容接口(interface)(据我了解典型的建议)。我没有等待它发生,而是尝试制作一个RAII包装器类,其析构函数调用DnsRecordListFree()并使用运算符重载转换来获取原始指针。但我对如何正确使用此句柄或指针来获取输出参

c++ - 具有 unique_ptr 的用户友好型 API

我正在为OpenGL实现一个简单的GUI,主要是作为我自己的练习。这个想法是有一个Gui类,其中每个实例都可以分配给不同的渲染目标(例如后台缓冲区或纹理)。GUI元素(小部件)恰好分配给Gui类的一个实例。我想在GUI中存储元素是unique_ptr的典型用例.这是我想出的:classElement{public:Element();virtual~Element();staticstd::unique_ptrcreate_unique();};classGui{public:typedefstd::unique_ptrelement_ptr;Gui();voidaddElement(

c++ std::unique_ptr 不会在 map 中编译

我目前正在尝试将std::unique_ptr存储在std::unordered_map中,但出现奇怪的编译错误。相关代码:#pragmaonce#include"Entity.h"#include#includeclassEntityManager{private:typedefstd::unique_ptrEntityPtr;typedefstd::mapEntityMap;EntityMapmap;public:/*AddsanEntity*/voidaddEntity(EntityPtr);/*RemovesanEntitybyitsID*/voidremoveEntity(i

c++ - 如何编写指针和数据为 const 的 unique_ptr

我第一次尝试编写一个基于范围的for循环来遍历unique_ptr时,我写道:std::vector>vec;//Initializevecfor(autov:vec)//error{}然后我意识到这是在尝试创建每个元素的拷贝,这对unique_ptr没有意义。所以后来我写了它作为引用:for(auto&v:vec){}在它前面添加一个const可以防止我更改指针。for(constauto&v:vec){v=nullptr;//error(good!)}要怎么写,才能让指向的数据不能改变?例如,以下代码不应编译。for(???v:vec){v->func();}classFoo{pu

c++ - std::unique_ptr 和 std::shared_ptr 之间破坏行为差异的基本原理是什么?

来自http://en.cppreference.com/w/cpp/memory/unique_ptr:IfTisderivedclass(sic)ofsomebaseB,thenstd::unique_ptrisimplicitlyconvertibletostd::unique_ptr.Thedefaultdeleteroftheresultingstd::unique_ptrwilluseoperatordeleteforB,leadingtoundefinedbehaviorunlessthedestructorofBisvirtual.Notethatstd::shared