草庐IT

unique_future

全部标签

c++ - 将 const std::unique_ptr 用于 pimpl 习惯用法

在HerbSutter'stalkatCppCon16他建议用conststd::unique_ptr编写pimplidiom(大约10分钟)。这应该如何与移动构造函数/赋值一起使用?c++17中有什么东西吗?我找不到任何东西。 最佳答案 如果您的类应该是永不为空的,那么非常量的唯一ptr(具有默认移动/分配)是不合适的。movector和moveassign都会清空rhs。一个constuniqueptr将禁用这些自动方法,如果你想移动,你必须在impl中编写它(并在外面有点胶水)。我会亲自编写一个具有我想要的语义的值ptr(然后

c++ - 带有派生类的 std::unique_ptr

我对c++11指针有疑问。具体来说,如何将基类的唯一指针转为派生类?classBase{public:intfoo;}classDerived:publicBase{public:intbar;}...std::unique_ptrbasePointer(newDerived);//now,howdoIaccessthebarmember?这应该是可能的,但我不知道怎么做。每次我尝试使用basePointer.get()我最终导致可执行文件崩溃。提前致谢,如有任何建议,我们将不胜感激。 最佳答案 如果它们是多态类型并且您只需要一个指

c++ - 为什么没有 unique_ptr::operator*() 的安全替代方案?

std::vector有成员函数at()作为operator[]的安全替代,因此可以应用边界检查并且没有创建悬空引用:voidfoo(std::vectorconst&x){constauto&a=x[0];//Whatifx.empty()?Undefinedbehavior!constauto&a=x.at(0);//Throwsexceptionifx.empty().}但是,std::unique_ptr缺少相应的功能:voidfoo(std::unique_ptrconst&x){constauto&a=*x;//Whatifbool(x)==false?Undefinedb

c++ - 为了支持 move 语义,函数参数应该由 unique_ptr、value 还是 rvalue 获取?

我的一个函数将vector作为参数并将其存储为成员变量。我正在使用对vector的const引用,如下所述。classTest{public:voidsomeFunction(conststd::vector&items){m_items=items;}private:std::vectorm_items;};但是,有时items包含大量字符串,所以我想添加一个支持move语义的函数(或用新函数替换该函数)。我正在考虑几种方法,但我不确定选择哪一种。1)unique_ptrvoidsomeFunction(std::unique_ptr>items){//Also,make`m_itm

c++ - std::unique_ptr vs std::shared_ptr vs std::weak_ptr vs std::auto_ptr vs 原始指针

与使用原始指针的类似(但不限于)一些高级技术相比,每个智能指针的等效用途是什么?我的理解很少,但从我能收集到的情况来看:原始指针:只有在你真的、真的、真的、真的知道你在做什么并且在界面后面小心地隐藏了用法的情况下才使用。std::auto_ptr:已过时永不使用。std::unique_ptr:在分配时转移所有权的单例指针。std::shared_ptr:引用计数指针,在分配时不会转移所有权,但会增加其引用计数。当所有引用离开作用域或显式std::shared_ptr::reset标的deallocator被调用。std::weak_ptr:子类型std::shared_ptr它不会增

c++ - 为什么 std::unique_lock 使用类型标签来区分构造函数?

在C++11中,std::unique_lockconstructor被重载以接受类型标签defer_lock_t、try_to_lock_t和adopt_lock_t:unique_lock(mutex_type&m,std::defer_lock_tt);unique_lock(mutex_type&m,std::try_to_lock_tt);unique_lock(mutex_type&m,std::adopt_lock_tt);这些是空类(类型标签)definedasfollows:structdefer_lock_t{};structtry_to_lock_t{};stru

c++ - 为什么我不能在 C++14 的 lambda 中 move std::unique_ptr?

我想在lambda中传递一个原始指针,但如果未调用lambda,我不希望它被泄露。它看起来像这样:voidClean(std::unique_ptr&&list);voidf(int*list){thread_pool.Push([list=std::unique_ptr(list)]{Clean(std::move(list));//我在Clang3.7.0中遇到错误:error:bindingofreferencetotype'unique_ptr'toavalueoftype'unique_ptr'dropsqualifiers但我一开始没有看到任何限定词,尤其是被丢弃了。另外,

c++ - C++ 对齐的 future : passing by value?

阅读Eigen库文档,我注意到someobjectscannotbepassedbyvalue.C++11中是否有任何开发或计划开发可以安全地按值传递此类对象?另外,为什么按值返回这样的对象没有问题? 最佳答案 完全有可能Eigen只是一个写得很糟糕的库(或者只是考虑不周);仅仅因为某些东西在线并不能使它成为现实。例如:PassingobjectsbyvalueisalmostalwaysaverybadideainC++,asthismeansuselesscopies,andoneshouldpassthembyreferenc

c++ - 删除函数 unique_ptr

我根本不明白错误输出,我写了一个生成它的类。用户队列.h#ifndefUSERQUEUES_H#defineUSERQUEUES_H#include#include#include#include"Job.h"classUserQueues{public:typedefstd::unique_ptrJobPtr;typedefstd::dequeJobDeque;public:UserQueues();voidprintDeques();voidaddToDeque(JobPtrjob,intqueueId);public:constuintQUEUE_QTY=3;private:st

c++ - 具有 std::unique_ptr 与 boost::ptr_container 的 STL 容器

有了c++11,我问自己是否可以替换c++11中的boost::ptr_containers。我知道我可以使用例如一个std::vector>,但我不确定这是否是一个完整的替代品。处理这些情况的推荐方法是什么? 最佳答案 我决定编写一个简短的程序,将一些多态对象放入一个容器中(通过指向堆的指针),然后将该容器与std::algorithm一起使用。我选择了std::remove_if只是一个例子。这是我将如何使用vector>:#include#include#includeclassAnimal{public:Animal()=d