草庐IT

unique_future

全部标签

c++ - 具有自定义删除器的 unique_ptr 构造函数被删除

这个例子在gcc4.8.3下编译和运行良好:#include#include#includeintmain(){autostr=newconstchar[6]{'h','e','l','l','o','\0'};std::unique_ptr>u_ptr(str,[](constchar*s){delete[]s;});std::cout但是当我尝试使用VisualStudioProfessional2013时,它无法编译(提示函数已删除)。这在VisualStudio2013中还不可能吗?还是我的示例代码有误而gcc忽略了我的错误?错误是:main.cpp(8):errorC2280

C++:将像 unique_ptr::get() 这样的参数传递给函数是否安全

传递像getAName(getA().get())这样的函数参数是安全的?getA()返回一个对象unique_ptr.我在VS2010上测试了下面的整个代码,它有效。但我想确定它是否是c++标准,与其他c++编译器一起使用是否安全?#include"stdafx.h"#include#includeusingnamespacestd;classA{public:A(){coutgetA(){returnstd::unique_ptr(newA());;}voidgetAName(A*a){if(a){coutname().c_str()控制台的输出是:A()A~()为了所有编译器的安

c++ - 是否可以使用 std::unique_ptr 来管理 DLL 资源?

我有很多LoadLibrary在我的项目中,需要调用FreeLibrary手动为每个LoadLibrary.我想使用std::unique_ptr具体deleter让它自动释放我的dll资源。这就是我要定义的:std::unique_ptrtheDll(LoadLibrary("My.dll"),FreeLibrary);但是编译器提示类型不匹配。我发现它期望*HMODULE来自LoadLibrary.即std::unique_ptr会期待A*作为它的指针类型。看起来我仍然需要定义一个新类来管理DLL资源(构造函数中的LoadLibrary和析构函数中的FreeLibrary)。有可能

c++ - 为什么 unique_ptr operator* 不是 noexcept?

这个问题在这里已经有了答案:"No-throwdereferencing"ofstd::unique_ptr(1个回答)关闭4年前。在为我的爱好操作系统实现一个基本的std库时,我遇到了这个并想知道为什么:operator->()和T*get()都被标记为noexcept,但是operator*()不是。根据引用资料,它应该等同于*get(),这将允许它成为noexcept并查看一些实现,我看不出为什么不是。为什么unique_ptr的取消引用运算符没有标记为noexcept?

c++ - 将 future 存储在列表中

我想将使用异步生成的多个线程的future存储在一个列表中,以便稍后检索它们的结果。futuref=async(doLater,parameter);list>l;l.push_back(f);但是编译器打印出如下错误信息/usr/include/c++/4.7/bits/stl_list.h:115:71:error:useofdeletedfunction'std::future::future(conststd::future&)[with_Res=int;std::future=std::future]'我是做错了什么还是列表不应该存储future?如果不是,应该使用什么?

c++ - boost::asio 与 boost::unique_future

根据http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/overview/cpp2011/futures.html,我们可以将boost::asio与std::future一起使用。但是我找不到任何关于使用boost::unique_future的信息,它有更多的功能,比如then()。我该如何使用? 最佳答案 Boost.Asio仅提供一流的异步操作支持,以返回C++11std::future或stackfulcoroutines中的实际值.尽管如此,requireme

c++ - make_unique 不编译

我正在尝试为std::unique_ptr创建和使用make_unique,就像std::make_shared存在于std::shared_ptrdescribedhere.赫伯萨特mentionsmake_unique的可能实现如下所示:templatestd::unique_ptrmake_unique(Args&&...args){returnstd::unique_ptr(newT(std::forward(args)...));}它似乎对我不起作用。我正在使用以下示例程序://testproject.cpp:Definestheentrypointfortheconsole

c++ - 使用带有 unique_ptr 的自定义删除器

借助shared_ptr,您可以使用自定义删除器,例如:autofp=shared_ptr(fopen("file.txt","rt"),&fclose);fprintf(fp.get(),"hello\n");这将记住fclose文件,无论函数如何退出。但是,引用局部变量似乎有点矫枉过正,所以我想使用unique_ptr:autofp=unique_ptr(fopen("file.txt","rt"),&fclose);但是,这不会编译。这是缺陷吗?有简单的解决方法吗?我是否遗漏了一些微不足道的东西? 最佳答案 应该是unique

C++ - vector<> 中的 std::unique_ptr 是 nullptr

我想将Particle对象存储在vector对象中,以便稍后访问它。这些粒子(Electrons和Protons)继承自包含toString()虚方法的Particle类。此toString()方法随后在Electron和Proton类中被覆盖。当我读取vector容器时,我想访问Electron或Proton特定的toString()方法,而不是粒子。显然,一种方法是使用std::unique_ptr。这是我尝试运行的代码的一部分:intmain(){/**/std::vector>particles(nbParticles);particles.push_back(std::uni

c++ - 在没有 std::move 的情况下如何按值返回 unique_ptr?

这个问题在这里已经有了答案:Returningunique_ptrfromfunctions(7个答案)关闭8年前。std::unique_ptrptr(){std::unique_ptrp(newint(3));returnp;//Whydoesn'tthisrequireexplicitmoveusingstd::move?}//Whydidn'tthedatapointedtoby'p'isnotdestroyedherethoughpisnotmoved?intmain(){std::unique_ptra=ptr();//Whydoesn'tthisrequirestd::m