草庐IT

c++ - std::unique_ptr<T[]> 和自定义分配器删除器

我正在尝试使用std::unique_ptr使用自定义内存分配器。基本上,我有自定义分配器,它们是IAllocator的子类,它提供了以下方法:void*Alloc(size_tsize)templateT*AllocArray(size_tcount)voidFree(void*mem)templatevoidFreeArray(T*arr,size_tcount)由于底层内存可能来自预分配block,因此我需要特殊的...Array()-分配和释放数组的方法,它们分配/释放内存并调用T()/~T()在范围内的每个元素上。现在,据我所知,std::unique_ptr的自定义删除器|

c++ - `std::vector< std::unique_ptr< T >>` 错误

我看到一些错误传递std::vector>周围有std::move.重现问题的代码是这样的:#include//forstd::unique_ptr#include//forstd::move#include//forstd::vectorstructbar{};usingvtype=std::vector>;structfoo{foo(vtypev):_v(std::move(v)){}private:vtype_v;};vtypegetVector(){return{std::move(std::unique_ptr(newbar()))};};intmain(){foof(std

c++ - std::any 由 std::exception_ptr

可能我不是第一个发现std::exception_ptr可用于实现any类型(性能考虑被搁置)的人,因为它是可能是C++中唯一可以容纳任何东西的类型。然而,谷歌搜索并没有在这方面带来任何结果。有人知道以下方法是否已在任何地方使用过吗?#include#includestructWrongTypeError:std::exception{};classAny{public:templatevoidset(Tt){try{throwt;}catch(...){m_contained=std::current_exception();}}templateTconst&get(){try{st

c++ - 在 vector 声明中移动 unique_ptr

这个问题在这里已经有了答案:CanIlist-initializeavectorofmove-onlytype?(8个答案)关闭5年前。我尝试在vector声明期间移动一些unique_ptr,但出现错误。我认为我在不知情的情况下进行了复制。我不明白为什么我在声明时遇到问题,而它在push_back期间工作得很好。我用几行简化了问题。#include#include#includeusingnamespacestd;intmain(){unique_ptri1=make_unique(142);unique_ptri2=make_unique(242);unique_ptri3=mak

c++ - 将 shared_ptr<T> 转换为 shared_ptr<void>

我有一个结构:structParams{std::shared_ptruser_data;/*...*/};我想这样使用它:intmain(){std::shared_ptrsp(newSpecializedParams(100));Paramsparams;/*...*/params.user_data=std::static_pointer_cast(sp);/*...*/std::shared_ptrsp2=std::static_pointer_cast(params.user_data);/*...*/return0;}这是有效且安全的吗? 最佳答

c++ - C++ 11 中 boost::scoped_ptr 的替代方案

我们刚刚将编译器升级到支持C++11的VC++2013。之前我们一直在使用来自Boost的shared_ptr和scoped_ptr类,但由于这是我们一直在使用的Boost类,我们正在寻找删除该依赖项。据我所知,std::shared_ptrs是boost::shared_ptrs的直接替代品,所以这(希望)很容易。但是,Boostscoped_ptrs的最佳替代品是什么(如果有的话)?会是unique_ptr吗?(老实说,虽然我写了代码,但那是大约10年前的事了,我已经忘记了使用scoped_ptrs的目的是什么......也许我只是在“玩”Boost,但到目前为止正如我所看到的,在

c++ - shared_ptr 和切片

与我共事的人曾经说过shared_ptr是不安全的,并且在从派生类转换为基类(即向上转换)时会切片。例如,如果有2个类A和B,其中B派生自A,则shared_ptra(newB)会切片。我给他指了http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/shared_ptr.htm它说的地方shared_ptrcanbeimplicitlyconvertedtoshared_ptrwheneverT*canbeimplicitlyconvertedtoU*.暗示在这些情况下使用它是安全的,但他似乎并不这么认为。

c++ - 在 Visual C++ 2010 上哪个更快 - std::shared_ptr 或 boost::shared_ptr?

有没有人在Release模式构建中测试过这个?还是实现如此相似而没有显着差异?我对速度感兴趣:创建一个新的shared_ptr创建shared_ptr的拷贝取消引用指针以访问指针对象这将在一个针对速度优化的发布版本中,使用make_shared()创建新的shared_ptr 最佳答案 好的,看来没有人这样做过。以下是我使用WIN32控制台应用程序的标准VC10优化设置发现的内容:VisualC++2010SP1std::make_shared和std::shared_ptr在填充包含1000万个指针条目的vector时比等效的Bo

c++ - 在 for range 循环中迭代包含 vector 的取消引用的 unique_ptr

为什么这段代码不像我想象的那样工作?for(autoit:*std::make_unique>(std::vector({1,2,3,4,5})))std::coutvector对象在执行循环的第一次迭代之前被销毁 最佳答案 range-basedforloop相当于:{init-statementauto&&__range=range_expression;...}对于您的range_expression,它将是auto&&__range=*std::make_unique>(std::vector({1,2,3,4,5}));但

c++ - 如何将派生类的 shared_ptr vector 转换为基类的 shared_ptr vector

classInterface{};classClass:publicInterface{};classFoo{public:std::vector>&GetInterfaces(){return*(std::vector>*)(&m_data);//returnm_data;}private:std::vector>m_data;};这行得通,但又丑又吓人。有更好/更安全的方法吗?我不想做m_data类型std::vector>因为模块Foo完全属于Class作品的,Interface(和Foo::GetInterfaces())被实现为与一个单独的模块交互,该模块应该只知道Inter