草庐IT

unique_future

全部标签

c++ - 使用 unique_ptr 进行依赖注入(inject)以模拟

我有一个使用Bar类的Foo类。Bar仅在Foo中使用,而Foo正在管理Bar,因此我使用unique_ptr(不是引用,因为我不需要Foo之外的Bar):usingnamespacestd;structIBar{virtual~IBar()=default;virtualvoidDoSth()=0;};structBar:publicIBar{voidDoSth()override{coutbar):bar_(std::move(bar)){}voidDoIt(){bar_->DoSth();}private:unique_ptrbar_;};目前一切顺利,一切正常。但是,当我想对代

c++ - std::make_unique、匿名命名空间和 ODR

请考虑以下测试用例(从LLVM源减少)://%catfoo1.cpp#includenamespace{classA{inti;};}classG{std::unique_ptrfoo()const;};std::unique_ptrG::foo()const{returnstd::make_unique();}和//%catfoo2.cpp#includenamespace{classA{boola;};}classH{std::unique_ptrbar()const;};std::unique_ptrH::bar()const{returnstd::make_unique();}

c++ - 为什么 make_unique 有一个可以将 std::bind 作为参数的构造函数的额外移动?

我有一个简单的类,它的构造函数如下所示:Event(std::function&&f):m_f(std::move(f)){}构造函数可以与std::bind一起使用:Thingthing;std::unique_ptrev(newEvent(std::bind(some_func,thing)));以上述方式使用它会导致“事物”的一个拷贝构造,然后在该拷贝上进行移动构造。但是,执行以下操作:std::unique_ptrev=make_unique(std::bind(some_func,thing));导致两个移动结构。我的问题是:什么时候调用“thing”的移动构造函数为什么用m

c++ - c++ 11中的 future vector

您好,我使用lambda函数在C++11中创建了一个futurevector。vectorv={0,1.1,2.2,3.3,4.4,5.5};autoK=[=](doublez){doubley=0;for(constautox:v)y+=x*x*z;returny;};vector>VF;for(doublei:{1,2,3,4,5,6,7,8,9})VF.push_back(async(K,i));它运行成功,但是当我尝试通过for_each调用检索值时,我遇到了一个我不理解的编译错误。for_each(VF.begin(),VF.end(),[](futurex){cout这些值

c++ - 在 C++/CLI 中使用 unique_ptr 时出现链接器错误

我目前正在转换我的auto_ptr实例至unique_ptr,但我遇到了一个问题。它在代码的C++部分工作得很好,但在我的托管C++/CLI层(该软件同时使用C#和C++)中执行它时,我遇到链接错误。它编译得很好,但在链接时会中断。auto_ptr从来没有任何问题.我目前正在使用VisualStudio2010。有人知道使用unique_ptr时遇到的任何问题吗?在C++/CLI中?我试图在下面的一段代码中总结我的问题,但请注意下面的代码实际上可以编译和工作(我检查了指针的所有权是否正确移动).编译时我没有收到链接错误,但下面的代码是纯C++而不是C++/CLI。我只是想提供一个代码构

c++ - 如何传递 unique_ptr<T> 代替原始 *output* 指针参数?

我在外部库中有一个预先存在的函数,它看起来像这样;boolCreateTheThing(MyThing*&pOut);简而言之;我给它一个原始指针(通过引用),函数分配内存并将我的指针分配给新分配的对象。当函数返回时,我有责任在我完成后释放内存。显然,我想将此结果存储到unique_ptr中,并避免使用手册delete.我可以创建一个临时原始指针以用于API调用,并将其传递到unique_ptr的构造函数中;MyThing*tempPtr;CreateTheThing(tempPtr);unique_ptrrealPtr=unique_ptr(tempPtr);还有比这更直接的方法吗?

c++ - 调用 get() 后 std::future 仍然有效(抛出异常)

根据cppreference,在调用std::future::get之后:valid()isfalseafteracalltothismethod.此外,来自cplusplus.com:Oncethesharedstateisready,thefunctionunblocksandreturns(orthrows)releasingitssharedstate.Thismakesthefutureobjectnolongervalid:thismemberfunctionshallbecalledonceatmostforeveryfuturesharedstate.在异常安全下:Th

c++ - 这是在 C++11 中使用 unique_ptr 和移动语义实现 pimpl 的正确方法吗

我还没有看到同时使用unique_ptr和移动语义的pimpl示例。我想向STL派生容器添加一个CHelper类,并使用pimpl隐藏CHelper的功能。这样看起来对吗?派生.hclassCDerived:publicset,publicCHelper{//...};`Helper.h//derivedcontainersneedtosupportbothcopyandmove,soCHelperdoestooclassCHelper{private:classimpl;unique_ptrpimpl;public://---default:needbothcotr&cotr(com

c++ - 与普通指针相比,按值传递 `unique_ptr` 是否会降低性能?

Commonwisdomisthatstd::unique_ptrdoesnotintroduceaperformancepenalty(andnotamemorypenaltywhennotusingadeleterparameter),但我最近偶然发现了一个讨论,该讨论表明它实际上引入了一个额外的间接寻址,因为unique_ptr无法在具有ItaniumABI的平台上的寄存器中传递。发布的示例类似于#includeintfoo(std::unique_ptru){return*u;}intboo(int*i){return*i;}Whichgeneratesanadditional

c++ - 从 unique_ptr<T[]> 初始化 shared_ptr<T>

[跟进this问题]最近我一直在处理指向C风格数组的智能指针。我最终完成了推荐的事情并改为使用指向vector的智能指针,但在那段时间里,我得到了一些建议:不要使用shared_ptr对象来管理最初使用make_unique创建的数组因为它不会调用delete[]而是delete.这对我来说似乎不合逻辑,我检查了两个Coliru和标准:这段代码:#include#includeintmain(){std::coutmyUnique(customArrayAllocator(4),customArrayDeleter);std::coutmyShared=std::move(myUniq