草庐IT

unique_future

全部标签

c++ - 为什么 std::unique_ptr 没有 operator<<?

C++11标准中的[util.smartptr.shared.io]要求operator为了shared_ptr小号:templatebasic_ostream&operator&os,shared_ptrconst&p);然而,除非我遗漏了它,否则我在[unique.ptr]中看不到任何类似的东西,而且引用en.cppreference.com同意。有没有理由区别? 最佳答案 Isthereareasonforthedifference?不,没有。与make_unique一样,这是一个“疏忽”,应该在将来添加(如果有人愿意发送提案

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++ - 在 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++ - 在 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++ - 使用 std::packaged_task 时 std::future 仍然延迟(VS11)

看来除非你调用std::async一个std::future绝不会设置为除future_status::deferred以外的任何其他状态除非你调用get或wait关于future。wait_for&wait_until将继续不阻塞并返回future_status::deferred即使任务已经运行并存储了结果。这是一个例子:#includevoidmain(){autofunc=[](){return5;};autoasyncFuture=std::async(std::launch::async,func);autostatus=asyncFuture.wait_for(std::

c++ - 为什么 C++ 异步在没有 future 的情况下按顺序运行?

#include#includevoidmain(){std::async(std::launch::async,[]{std::cout在这段代码中,只会输出“async...”,也就是说这段代码是阻塞在async的。但是,如果我添加future并让语句变为:std::futurefut=std::async([]{std::cout然后一切顺利(不会阻塞)。我不确定为什么会这样。我认为异步应该在单独的线程中运行。 最佳答案 来自encppreference.com:Ifthestd::futureobtainedfromstd:

c++ - 为什么 unique_ptr 析构函数的异常会终止程序?

看看这段代码,它导致程序在未捕获异常的情况下终止。#include#include#include#includeusingnamespacestd;structtest{~test()noexcept(false){throwruntime_error("-my-cool-exception-");}};intmain(){try{autoptr=unique_ptr(newtest());//testt;//thisisok,withoutunique_ptritworksfine.}catch(exception&e){cout这个问题不同于堆栈溢出问题:throwingexce

c++ - 使用 std::unique_ptr 将所有权转移到函数

我正在努力学习如何使用智能指针和理解所有权。当我按值将auto_ptr传递给函数时,该函数获得该指针的独占所有权。因此,当函数完成时,它会删除我传递给它的指针。但是,当我尝试使用unique_ptr执行此操作时出现编译错误,就好像unique_ptr的复制分配被禁用一样。通过引用传递unique_ptr似乎并没有转移所有权,它只是为函数提供了对unique_ptr的引用。如何通过将所有权传递给函数来获得auto_ptr的行为以使用unique_ptr?如果能提供有关unique_ptr的详细教程的链接,我将不胜感激,因为到目前为止,我读过的教程似乎只谈论auto_ptr或谈论Boost

c++ - std::remove_if 来自 std::vector 的多态 std::unique_ptr

我有一个包含三个类的层次结构,其中Derived源自Selectable和Drawable.然后我有一个std::vector的std::unique_ptr我用Derived填充对象。我确定该vector将仅由同时从两个基派生的对象填充。当我尝试使用指向Selected的指针从vector中删除某个元素时,问题就来了.#include#include#includestructSelectable{virtual~Selectable()=0;};Selectable::~Selectable()=default;structDrawable{virtual~Drawable()=0