草庐IT

SOME_UNIQUE_VALE

全部标签

c++ - 用于将 weak_ptr 应用于 unique_ptr 的内容

我试图理解c++11中的unique_ptr、shared_ptr和weak_ptr。我听说weak_ptr对于缓存和中断循环等事情会很好。我听说它们与shared_ptrs配合得很好。但是在这方面,shared_ptrs和unique_ptrs有什么区别呢?为什么weak_ptr只能与一个一起使用而不能与另一个一起使用?为什么我不想对其他人拥有的东西进行弱引用? 最佳答案 weak_ptr在技术上是一种卡在管理某些共享对象的一组shared_ptr的引用计数器上的方法。当最后一个shared_ptr被销毁时,对象被销毁,但只要有w

c++ - 在 unique_ptr 上使用强制转换运算符是否危险?

我们有一个广泛的代码库,目前使用原始指针,我希望迁移到unique_ptr。但是,许多函数期望原始指针作为参数,并且在这些情况下不能使用unique_ptr。我意识到我可以使用get()方法来传递原始指针,但这会增加我必须接触的代码行数,而且我觉得它有点难看。我推出了自己的unique_ptr,如下所示:templateclassmy_unique_ptr:publicunique_ptr{public:operatorT*(){returnget();};};然后每次我向需要原始指针的函数parm提供my_unique_ptr时,它都会自动将其转换为原始指针。问题:这样做有什么内在的

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::function<some_type_t, void> 无效?

在C++中,如果我尝试这样做:std::function然后编译器会抛出错误。为什么是这样?它在许多情况下很有用。一个例子://g++-std=c++17prblm.cpp#include#includetemplateclasssome_callback{public:usingcallback_t=std::function;some_callback(callback_t_myfunc){this->myfunc=_myfunc;}callback_tmyfunc;};usingcallback_with_just_bool=some_callback;usingcallback

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++ - '从 some_type** 到 const some_type** 的无效转换'

我有一个函数需要constsome_type**作为参数(some_type是一个结构,函数需要一个指向这种类型数组的指针).我声明了一个some_type*类型的局部变量,并对其进行了初始化。然后我将该函数称为f(&some_array),编译器(gcc)说:error:invalidconversionfrom‘some_type**’to‘constsome_type**’这里有什么问题?为什么我不能将变量转换为常量? 最佳答案 参见:Whycan'tIpassachar**toafunctionwhichexpectsaco