草庐IT

unique_value

全部标签

c++ - 如何对数组使用 unique_ptr

我有课classA{public:A(){couta(newA[5]);//-doesn'tworkunique_ptra(newA[1]);//-doesn'tworkunique_ptra(newA);//-works}为什么会这样?我猜是关于移动构造函数的(由于析构函数,它不能自动创建),但是为什么我们在这里需要一个移动构造函数?和之间有什么区别:unique_ptra(newA[1]);//-doesn'tworkunique_ptra(newA);//-works 最佳答案 要将unique_ptr与数组分配一起使用,您需

c++ - 警告 : second/third operand of conditional has no effect [-Wunused-value]

std::cout我想检查给定值是否可以创建三角形。我收到警告:secondoperandofconditionalexpressionhasnoeffect[-Wunused-value]thirdoperandofconditionalexpressionhasnoeffect[-Wunused-value]怎么了? 最佳答案 您的代码转换为:((std::cout首先,operator有更高的operatorprecedence比operator&&.只有abs(b-c)的值将被打印并且(a部分将与std::ostream::

c++ - 为什么 std::map 没有 insert(key &, value & v) 类型的插入函数

为什么std::map不支持如下插入:std::mapmap_int;voidinsert_map(obj1&key,obj2&val){map_int.insert(key,val);}我知道以上是不正确的。我想知道是什么阻止了这样设计插入功能。它比创建一对IMO更直观。 最佳答案 它叫做emplace():std::mapm;//usespair'stemplateconstructorm.emplace("d","ddd"); 关于c++-为什么std::map没有insert(k

c++ - 我可以将函数的输出参数存储到 unique_ptr 中吗?

我有以下代码:classThing{};voidfnc(Thing**out){*out=newThing();};其中fnc通过输出参数返回Thing的新实例。通常我会按如下方式使用它:intmain(){Thing*thing;fnc(&thing);}我可以将返回的对象放在std::unique_ptr中吗?intmain(){std::unique_ptruniqueThing;fnc(???);} 最佳答案 要扩展您的代码示例,它(即传递指针)将是voidfnc(std::unique_ptr*out){out->rese

c++ - 如何用空指针初始化 unique_ptr 的 vector ?

我需要初始化一个vector>与nullptr秒。this中的方法帖子太复杂了。我的情况比较特殊,只需要初始化为nullptr.我怎样才能实现它?我知道我可以使用for循环来push_back一个nullptr每一次。有什么优雅的方法吗?顺便说一句,make_unqiue不适用于我的编译器。#include#include#includeusingnamespacestd;structTNode{//charch;boolisWord;vector>children;TNode():isWord(false),children(26,nullptr){}};intmain(){TNod

c++ - 单元测试、模拟和 unique_ptr

有一个正在测试的类目前接受unique_ptr&&在它的构造函数中,表示它想要获得接口(interface)实现的单一所有权。想要使用模拟Interface测试此类时会出现问题虽然:模拟框架(HippoMocks)只给我Interface*我不拥有,因此无法删除。我以前在测试constshared_ptr&的类(class)时遇到过同样的问题作为参数,但通过提供自定义的无操作删除器来修复:templatevoidNoDelete(T*){}//createashared_ptrwithouteffectivedeletertemplatestd::shared_ptrmock_shar

c++ - std::unique_ptr 和指向指针的指针

我想结合使用std::unique_ptr和FreeImage的FITAG。普通C中的代码将是:...loadimage;FITAG*tag=NULL;FreeImage_GetMetadata(FIMD_EXIF_EXIF,bitmap,"Property",&tag);...dosomestuffwithtag;FreeImage_DeleteTag(tag);...deleteimage;我对unique_ptr的尝试:std::unique_ptrtag(NULL,&FreeImage_DeleteTag);FreeImage_GetMetadata(FIMD_EXIF_EXI

c++ - 当shared_ptr,当unique_ptr

这个问题在这里已经有了答案:Differencesbetweenunique_ptrandshared_ptr[duplicate](4个答案)关闭7年前。什么时候应该使用shared_ptr什么时候使用unique_ptr?例如在这个类中而不是node*应该是shared_ptr或unique_ptr。它取决于什么?classnode{private:node*parent;vectorchildren;/**txny*x-numerdrzewa*y-numerwezla*/stringid;typeNodetype;//0-term,1-funcpublic:node(node*p

c++ - 将 std::unique_ptr 重置为指向数组的指针有什么问题?

我看到如下代码片段:std::unique_ptrmCache;mCache.reset(newuint8_t[size]);有人告诉我这段代码有一些问题。谁能给我一些细节? 最佳答案 给定std::unique_ptrmCache;,当mCache被摧毁了它的deleter将使用delete销毁被管理的指针(如果有的话),即为单个对象释放内存。但是在mCache.reset(newuint8_t[size]);之后什么mCachemanages是指向数组的指针,这意味着它应该使用delete[]反而;使用delete为数组释放内存

C++ "value++"会导致警告但 "value+1"不会?

这个问题在这里已经有了答案:Undefinedbehaviorandsequencepoints(5个答案)关闭3年前。代码:inta=0;a=++a%5;引起警告:warning:operationon'a'maybeundefined[-Wsequence-point]a=++a%5;~~^~~~~~~~~用-Wall编译时用各种编译器比如gcc然而这段代码,工作正常吗?inta=0;a=(a+1)%5;为什么这是一个警告,可以安全地忽略它吗?将其包裹在方括号中似乎并不能使警告消失。编辑:为澄清起见,我在看到这些警告消息时使用的是C++17编译器。