草庐IT

unique_future

全部标签

c++ - 使用 std::unique_ptr<T>& 而不是 std::unique_ptr<T> 有什么优势吗?

使用std::unique_ptr&有什么好处吗?而不是std::unique_ptr?例如,在函数参数中? 最佳答案 有几种不同的情况您想将所有权转移给函数使用std::unique_ptr您想允许函数修改指针使用std::unique_ptr&您想允许函数修改指针对象使用T&,并在调用站点取消引用如果指针可能为空,则使用T*并调用unique_ptr::get在调用站点你想让函数观察指针对象使用constT&,并在调用站点取消引用如果指针可能为空,则使用constT*并调用unique_ptr::get在调用站点你想让函数拥有指

c++ - 在 BST 中使用 unique_ptr 而不是 shared_ptr

我正在尝试使用unique_ptr实现BST。我得到了shared_ptr的工作程序。我该如何着手使用unique_ptr来强制执行BinarySearchTree的单一所有权语义?当我将shared_ptr替换为unique_ptr时,出现了我无法理解的编译错误。#include#includetemplateclassBinarySearchTree{structTreeNode;typedefstd::shared_ptrspTreeNode;structTreeNode{Tdata;spTreeNodeleft;spTreeNoderight;TreeNode(constT&v

c++ - 将 std::unique_ptr 传递给辅助函数

这是正确的方法吗?voidhelperFunc(MyClass*ptr){//dosomethingwithptr,}unique_ptrp(newMyClass());helperFunc(p.get());或者我应该使用shared_ptr进行此类操作吗? 最佳答案 除非你想获得内存的所有权(在这种情况下你应该传递shared_ptr或unique_ptr),你为什么要使用指针?通过引用传递。voidhelperFunc(MyClass&obj){//dosomethingwithptr,}unique_ptrp(newMyCl

c++ - 试图返回一个用 `std::unique_ptr` 构造的 `NULL`

我正在设计一个创建不同类型Foo的工厂,并且我正在尝试使用智能指针。大多数似乎都运行良好,但由于编译器的限制,我缺少一些重要的功能(即nullptr)。我有这个方法:std::unique_ptrcreateFoo(conststd::string&fooType){autoit=_registeredFoo.find(fooType);//_registeredFooisamapif(it!=_registeredFoo.end())returnstd::unique_ptr(it->second());returnstd::unique_ptr(NULL);}当我测试这个方法时,它

c++ - 由 const std::unique_ptr 管理的对象的生命周期

我在std::unique_ptrreference中看到以下注释:Onlynon-constunique_ptrcantransfertheownershipofthemanagedobjecttoanotherunique_ptr.Thelifetimeofanobjectmanagedbyconststd::unique_ptrislimitedtothescopeinwhichthepointerwascreated.有谁能举例说明一下吗?我不明白为什么。 最佳答案 您根本无法从conststd::unique_ptr移动,

c++ - 为什么 std::future::wait_for 不等待正确的持续时间?

我不明白为什么调用std::future::wait_for时测量的持续时间和指定的持续时间之间的差异会随着指定持续时间的增加而增加。当我告诉std::future等待10ns并测量耗时时,我得到~2000ns。现在,10纳秒是一个非常短的持续时间,所以可能相关函数调用涉及太多开销以等待这么短的时间。但是当我告诉std::future等待100000ns并测量耗时时,我得到~150000ns。分别等待10微秒和100微秒时,可以看到类似的效果。#include#include#include#includeusingnamespacestd::chrono;usingnamespace

c++ - 为什么我们不能将 "="用于 shared_ptr 或 unique_ptr?

这个问题在这里已经有了答案:Whydoesn'tshared_ptrpermitdirectassignment(5个答案)关闭5年前。我发现语法有编译错误。std::shared_ptrp=newint(5);3141E:\temprory(deleteitifulike)\1208.cpp[Error]conversionfrom'int*'tonon-scalartype'std::shared_ptr'requested不过没关系std::shared_ptrp(newint(5));与unique_ptr相同;但不知道为什么要禁止。

c++ - 将执行从一个线程转移到另一个线程以实现任务并行性和 future 调用

我正尝试在C++中实现一个future调用机制。虽然这只是一个测试代码(制作有点匆忙),但我打算在我正在使用的一种语言的运行时使用类似的东西来实现透明并行。我已经干掉了我正在处理的代码以使其更小一些,尽管它仍然很大:#include#include#include#include#include#include#include#include#include#includeusingnamespacestd;usingnamespacestd::chrono;//--------------------------------------------------------------

c++ - 为什么我不能用 reset() 删除 unique_ptr<char[]>?

当我有一个指向单个对象的唯一指针时,我可以用reset()删除它:std::unique_ptrvariable(newchar);variable.reset();但是,这不适用于std::unique_ptr包含一个数组。为什么?删除此类指针的正确方法是什么?我正在使用EmbarcaderoC++Builder10.1。相关标准是C++11。我的观察当我有一个包含数组的唯一指针时,编译失败:std::unique_ptrvariable(newchar[10]);variable.reset();错误信息是nomatchingfunctiontocallfor'reset'.这也失

c++ - `unique_lock`、 `scoped_lock` 和 `lock_guard` 中指定的 mutex_type 的用例是什么?

用于保护std::mutex的c++11mutexRAII类型都有一个typedef:typedefMutexmutex_type;std::lock_guard::mutex_typestd::unique_lock::mutex_typestd::scoped_lock::mutex_type这个成员typedef有什么意义?起初我认为它可以用来概括创建一个对象来移动锁(在unique_lock的情况下)例如:templatevoidfunction(SomeLockin)SomeLock::mutex_typenewMutex;//Dosomething但我无法想象它的用途。需要