jjzjj

c++ - 动态分配数组的 unique_ptr 替代方案

我写了一个类,它必须与一些需要一些C风格数组(或至少指向第一个元素的指针)作为参数的旧代码接口(interface)。这些数组是我类的成员,它们特别大(50kb)所以我想把它们放在堆上,这样我类的对象在堆栈上就不会很大。我非常相信使用资源管理对象,所以我宁愿自己不在堆上管理这些数组。我发现使用unique_ptr的效果特别好。例如:std::unique_ptrsomeArrayName并使用:someArrayName(newSOMETYPE[someLargeSize])在我的构造函数的初始化列表中。这允许我使用.get()将它们用作常规C数组需要它作为参数的函数的方法,我不必自己

c++ - 为什么 GCC 给我一个错误 : no unique final overrider?

在下面的代码中,我收到以下警告和错误:test.cpp:15:warning:directbase'B'inaccessiblein'D'duetoambiguitytest.cpp:15:error:nouniquefinaloverriderfor'virtualvoidA::f()'in'D'但是如果我从A中移除B的虚拟继承(即structB:publicA),我只会得到警告,没有错误。structA{virtualvoidf()=0;};structB:publicvirtualA{voidf(){}};classC:publicB{};structD:publicC,virt

c++ - 为什么 list<unique_ptr> 中的 unique_ptr 的 std::move() 没有真正 move 它?

usingPtr=std::unique_ptr;Ptrf(boolarg){std::listlist;Ptrptr(newint(1));list.push_back(std::move(ptr));if(arg){Ptr&&obj1=std::move(list.front());//Here|obj1|and|list.front()|stillpointtothesamelocation!list.pop_front();returnstd::move(obj1);}else{Ptrobj2=std::move(list.front());list.pop_front();r

c++ - 将 unique_ptr 的 vector 传递给对象。 vector 成为成员变量。正确的做法?

我将称之为“所有者”的一个对象在其生命周期内拥有数据对象vector的明确所有权。这些存储为unique_ptr的vector。一个对象/类,称为“Output”,需要在许多不同的方法中查看这些数据对象,因此某种引用/指针/变量是“Output”的成员变量.Output在其构造函数中接收数据对象的vector。我想到了三种方法来实现这一点。最好的方法是什么?选项1-“输出”对象将数据vec存储为常量引用:classOutput{//outputwantsthedata:public:Output(std::vector>const&in):my_lot_of_data(in){};st

c++ - 具有 unique_ptr 作为成员的 C++ 模板的构造函数失败

当类模板包含指向另一个类的unique_ptr时,该类的构造函数不会将unique_ptr移动到新对象中。使用相同的类,但没有模板,构造函数按预期生成对象。#includeclasstest1{public:std::strings_;test1(std::strings):s_(s){};};classtestu{public:std::unique_ptrus_;testu(std::unique_ptrus):us_(std::move(us)){};};templateclasstestt{public:std::unique_ptrus_;testt(std::unique_

c++ - 用于在 auto_ptr 和 unique_ptr 之间切换的宏

在一个仍然使用C++11之前版本的项目中,我想通过使用C++11编译器进行编译并修复错误来为切换准备源代码。它们包括std::auto_ptr的实例替换为std::unique_ptr必要时,用std::move()包裹智能指针一些0和NULL替换为nullptr现在我想切换回C++之前的编译器并编写一个可以切换回更改的宏,这样,当最后的编译器切换时间到了时,我只需删除宏。我试过了#ifndefHAVE_CXX11#definenullptrNULLnamespacestd{#defineunique_ptrauto_ptr}#endif(使用exvector与智能指针一起使用的示例类

c++ - 如何修复模板内的错误重构 decltype

编辑可能无法完成,请参阅Cleanimplementationoffunctiontemplatetakingfunctionpointer虽然答案1有一个C宏解决方法https://stackoverflow.com/a/18706623/2332068我将一个函数传递到模板中以成为构造函数的预提供参数,但还需要使用decltype在该函数上将函数类型传递给unique_ptr模板实例化器(?这个词对吗)如果我预先使用decltype就可以了作为一个额外的模板参数,但如果我在作为参数传递的函数的模板中调用它,则不会。我正在使用g++4.9.2,并在此处扩展我的探索Callinginh

c++ - std::unique_ptr 使用带有少量参数的自定义删除器

我想知道是否可以使用多个参数(标准删除器签名)为std::unique_ptr指定自定义删除器。我知道std::shared_ptr存在std::bind的解决方法,这使得它成为可能但是std::unique_ptr存在一些技巧吗?对我来说似乎不是因为根据http://en.cppreference.com/w/cpp/memory/unique_ptr:Typerequirements-DeletermustbeFunctionObjectorlvaluereferencetoaFunctionObjectorlvaluereferencetofunction,callablewit

c++ - unique_ptr refcount 的替代方案

在下面的代码示例中,只要B的任何对象存在,就应该在structB中存在一个structA的实例.示例按预期工作。#include#include#includestructA{A(){std::coutguard(mtx);if(!refCount){a.reset(newA);}++refCount;}~B(){std::coutguard(mtx);--refCount;if(!refCount){a.reset();}}staticstd::unique_ptra;staticstd::mutexmtx;staticintrefCount;};std::unique_ptrB::

c++ - boost std unique_ptr 的 STL 集合的序列化

我希望能够序列化std::unique_ptr的STL容器。可以吗?顺便说一句,单个std::unique_ptr一切正常。下面是我正在处理的代码,gcc给出了以下错误:useofdeletedfunction‘std::unique_ptr::unique_ptr(conststd::unique_ptr&)[with_Tp=MyDegrees;_Dp=std::default_delete;std::unique_ptr=std::unique_ptr]’如何使代码正常工作?#include#include#include#include#include#include#inclu