草庐IT

unique_future

全部标签

c++ - 使用 C++11 时扩展命名空间 std 以实现 make_unique

我遇到了一个固定在C++11功能上但实现了std::make_unique的代码库。如果不使用C++14,则扩展namespacestd以添加功能,即围绕实现包装#ifdefined(__cplusplus)&&__cplusplus我知道那是undefinedbehaviortoextendnamespacestd(有一些异常(exception))。上述情况仍然可以接受还是应该避免? 最佳答案 不,这是被禁止的——尽管,通过#definemake_unique???符合标准的C++11程序可以非常确定库从不提及名称(在字符串化之

c++ - 将函数标记为虚拟会导致 unique_ptr 出现编译器错误

我有一个包装vector的模板类。我试图在这个类中存储unique_ptrs,它工作正常。但是,当我将voidadd(constT&elem)函数标记为虚拟时,我的编译器(clang)告诉我我正在为unique_ptr进行“调用隐式删除的复制构造函数”。我知道无法复制unique_ptr,所以我创建了voidadd(T&&elem)函数。我只是不知道为什么将另一个add函数标记为virtual会导致编译器错误。感谢您的宝贵时间。#include#include#includeusingnamespacestd;templateclassContainerWrapper{private:

c++ - Boost.Pointer 容器在 C++11/14 中被 std::unique_ptr 淘汰了吗?

是否std::unique_ptr使Boost.PointerContainer库在C++11/14中过时?在C++98/03中没有移动语义,也没有像shared_ptr这样的智能指针。与原始指针相比,具有引用计数相关的开销(对于引用计数block和互锁增量/减量)。所以像std::vector>如果与std::vector相比有开销.但是是std::vector>与std::vector一样高效(没有引用计数开销),和此外安全关于异常和自动销毁(即vector>析构函数将自动调用析构函数对于指针存储在T中的vector项)?如果是这样,Boost.PointerContainer在C

c++ - 对使用 unique_ptr 和自定义删除器感到困惑

我正在尝试使用unique_ptr使用SDL_Surface的自定义删除器类型。这只是一个使用int的示例打字,但我希望你明白了。#include#include#includetypedefintSDL_Surface;SDL_Surface*CreateSurface(){SDL_Surface*p=newSDL_Surface;returnp;}voidFreeSurface(SDL_Surface*p){deletep;}intmain(){std::unique_ptr>uptr_1;//howtoassignavaluetouptr_1andthedeleter?retur

c++ - 成员初始化列表中的 unique_ptr

编辑:我知道unique_ptr是不可复制的,只能移动。我不明白初始化列表会发生什么。为什么成员初始化列表中的unique_ptr可以像代码片段中那样工作?#includeclassMyObject{public:MyObject():ptr(newint)//thisworks.MyObject():ptr(std::unique_ptr(newint))//ifoundthisinmanyexamples.butwhythisalsowork?//ithinkthisisusingcopyconstructorasthebottom.{}MyObject(MyObject&&oth

c++ - 将 std::unique_ptr 与标准容器一起使用

当我意识到C++11添加了unique_ptr时,我一直在寻找一种方法来实现安全vector和动态指针映射。我研究了如何在Google上使用它们,但未能找到详细信息。我需要知道的是:除了自动内存收集之外,指针和unique_ptr之间到底有什么不同?如何从vector或map中删除unique_ptr?除了删除迭代器之外,我还必须使用任何特殊代码吗? 最佳答案 没有。unique_ptr只是一个指针的包装器,它会在unique_ptr被销毁时删除指针。它没有开销(就像它替换的auto_ptr模板一样)。不,它会起作用的。困难实际上来

c++ - 使用 unique_ptr 控制文件描述符

理论上,我应该能够使用自定义指针类型和删除器,以便让unique_ptr管理一个不是指针的对象。我尝试了以下代码:#ifndefUNIQUE_FD_H#defineUNIQUE_FD_H#include#includestructunique_fd_deleter{typedefintpointer;//Internaltypeisapointervoidoperator()(intfd){close(fd);}};typedefstd::unique_ptrunique_fd;#endif//UNIQUE_FD_H这不起作用(带有-std=c++11参数的gcc4.7)。它响应以下错

c++ - 尝试从 vector 中获取 unique_ptr 元素后出错

我阅读了有关如何插入unique_ptr的信息进入vector>:WhycanInotpush_backaunique_ptrintoavector?但是我如何取回一个元素呢?我举了个例子:#include#includeclassA{public:A(intx,inty){x_=x;y_=y;}private:intx_;inty_;};intmain(){std::vector>vec_a;std::unique_ptrtmp_a=std::unique_ptr(newA(13,32));vec_a.push_back(std::move(tmp_a));vec_a.push_ba

c++ - 为什么我不能在 std::future 参数中使用引用

为什么以下代码(onIdeone)会给我一个错误?#include#include#includeintmain(){intfoo=0;boolbar=false;std::futureasync_request=std::async(std::launch::async,[=,&foo](bool&is_pumping_request)->std::string{return"str";},bar);std::cout输出:Infileincludedfrom/usr/include/c++/5/future:38:0,fromprog.cpp:1:/usr/include/c++/

c++ - unique_lock 与使用互斥锁相比有什么特殊用途?

我不太清楚为什么std::unique_lock比仅使用普通锁有用。我正在查看的代码示例是:{//aquirelockstd::unique_locklock(queue_mutex);//addtasktasks.push_back(std::function(f));}//releaselock为什么这个比queue_mutex.lock();//addtask//...queue_mutex.unlock();这些代码片段是否完成了同样的事情? 最佳答案 [Do]thesesnippetsofcodeaccomplishthe