草庐IT

together_unique

全部标签

c++ - 为什么 unique_ptr 对 auto_ptr 有重载?

我遇到编译器错误并注意到一些有趣的事情。出于某种原因,unique_ptr对auto_ptr有重载,但我认为auto_ptr已被弃用:/usr/local/include/c++/4.9.0/bits/unique_ptr.h:228:2:note:templatestd::unique_ptr::unique_ptr(std::auto_ptr&&)unique_ptr(auto_ptr&&__u)noexcept;/usr/local/include/c++/4.9.0/bits/unique_ptr.h:228:2:note:templateargumentdeduction/s

c++ - 为什么 std::make_unique 等不存在 std::initializer_list 重载?

参见ShouldIuse()or{}whenforwardingarguments?.foo是std::vector克隆。在N4140中,unique.ptr.createstd::make_unique指定为:templateunique_ptrmake_unique(Args&&...args);Remarks:ThisfunctionshallnotparticipateinoverloadresolutionunlessTisnotanarray.Returns:unique_ptr(newT(std::forward(args)...)).这意味着需要实现才能使用()而不是{

c++ - 在什么时候将 unique_lock 与 shared_mutex 一起使用?

通常,当使用“普通”互斥量时,您会像在remove1()中那样使用它。但是,现在有了shared_lock和unique_lock,是否应该先使用共享锁,只有在必要时才使用唯一锁?请注意,当模型不存在时,remove()可能不需要unique_lock。voidremove1(intid){std::unique_locklock(mutex_);for(autoit=models_.begin();it!=models_.end();++it)if((*it)->getId()==id){it=models_.erase(it);return;{}voidremove2(intid)

c++ - 如何让这段涉及 unique_ptr 的代码进行编译?

#include#includeusingnamespacestd;classA{public:A():i(newint){}A(Aconst&a)=delete;A(A&&a):i(move(a.i)){}unique_ptri;};classAGroup{public:voidAddA(A&&a){a_.emplace_back(move(a));}vectora_;};intmain(){AGroupag;ag.AddA(A());return0;}不编译...(说unique_ptr的复制构造函数被删除)我尝试用forward替换move。不确定我这样做是否正确,但它对我不起作

C++如何将unique_ptr的队列添加到 vector

简化代码:#include#include#includeclassFoo{public:Foo(){};virtual~Foo(){}};intmain(){std::queue>queue;autoelement=std::make_unique();queue.push(std::move(element));std::vector>>vector;//Error1vector.push_back(queue);//Error2vector.push_back(std::move(queue));//Error3vector.push_back({});return0;}错误:'

c++ - std::unique_ptr 结构成员到结构类型

是structA{std::unique_ptra;};标准允许吗?我不认为它适用于像std::set这样的容器类型,但是关于unique_ptr有什么特别吗? 最佳答案 是的,这是明确允许的。C++14(n4140)20.8.1/5:...ThetemplateparameterTofunique_ptrmaybeanincompletetype.std::shared_ptr和std::weak_ptr也允许使用类似的措辞。 关于c++-std::unique_ptr结构成员到结构类

c++ - unique_ptr 的 deque vector 的编译器错误

以下代码无法在gcc5.3上编译,编译器错误提示unique_ptr的复制构造函数以某种方式被调用。有人可以解释为什么会这样吗?#include#include#includeusingFoo=std::deque>;voidfoo(){std::vectora;a.emplace_back();//thisfailstocompile}编译错误中的关键行是:gcc-4.9.2/include/c++/4.9.2/bits/stl_construct.h:75:7:error:useofdeletedfunction‘std::unique_ptr::unique_ptr(consts

c++ - 将 unique_ptr<Object> 作为 unique_ptr<const Object> 返回

我有这样的方法:std::unique_ptrTable::GetStats()const{std::unique_ptrresult;//...//Preparestats.Undersomeconditionsanexceptionmaybethrown.//...returnresult;}问题是它无法编译:error:cannotbind‘std::unique_ptr’lvalueto‘std::unique_ptr&&’我可以使用以下绕过方法使其编译:returnstd::unique_ptr(result.release());不过好像有点过分了。我无法理解,从C++的角

C++, Linux : error: conversion from ‘boost::unique_future<void>’ to non-scalar type ‘boost::shared_future<void>’ requested. 如何绕过它?

我尝试使用boostthreadfutures.所以如图here我们可以得到sharedfuture来自packagedtask.所以我在linux上尝试这样的功能:templatevoidpool_item(boost::shared_ptr>pt){boost::shared_futurefi=pt->get_future();//error//...但调用它时出错:../../src/cf-util/thread_pool.h:Inmemberfunction‘voidthread_pool::pool_item(boost::shared_ptr>)[withtask_retu

c++ - std::vector<std::unique_ptr<T>> 有更好的替代方案吗?

我正在寻找需要满足以下要求的容器(针对游戏开发,尤其是实体管理):快速迭代没有存储元素的拷贝不会使指向元素的指针失效删除和插入元素例子:Containercontainer;//ThispointerwillalwayspointtotheplayerEntity*player{newEntity};container.add(player);//Setsomeentitiesto"dead"for(auto&e:container)if(e->type=="Enemy")e->die();//Useerase-removeidiomon"dead"entitiescontainer.