草庐IT

unique_random_numbers

全部标签

c++ - 将 std::unique_ptr 传递给类时出错

我必须创建从抽象类继承的类的实例。我的代码非常简单。它应该基于抽象类创建对象类的实例。抽象类也是模板类。然后我需要将这个对象放入保存指向该对象的指针的存储类中。传递指针时出现错误:templates.cpp:Inmemberfunction‘voidstorage::setPTR(std::unique_ptr&)’:templates.cpp:39:28:error:useofdeletedfunction‘std::unique_ptr&std::unique_ptr::operator=(conststd::unique_ptr&)[with_Tp=child;_Dp=std::

c++ - 通过 unique_ptr 和自定义删除器使用自动扣除

我目前正在使用一个C库,该库定义了许多数据类型,所有这些类型都需要由用户管理它们的生命周期。有许多函数以这种方式定义:int*create(){returnnewint();}voiddestroy(int*i){deletei;}其中大部分在创建后不需要访问。他们只需要存在。因此,我尝试使用在我需要它们存在的范围内声明的unique_ptr来管理它们。这样的声明是这样的://NotethatI'mavoidingwritingthetype'snamemanually.autoa=std::unique_ptr,decltype(&destroy)>{create(),&destro

c++ - 动态分配 std::unique_ptr 有什么用?

使用new创建一个std::unique_ptr是否有意义?在下面的代码片段中,我怀疑std::unique_ptr管理的SimpleClass对象不会被销毁,除非我删除std::unique_ptr我自己。我想不出它在什么情况下有用,所以我想知道是否存在实际使用它的情况。std::unique_ptr*ptr_to_unique_ptr=newstd::unique_ptr();ptr_to_unique_ptr->reset(newvector_test::SimpleClass(555));deleteptr_to_unique_ptr; 最佳答案

c++ - 使用带有自定义释放器的 std::unique_ptr 来包装原始指针

我正在尝试使用libsvm对于某个复杂的应用程序,并且由于libsvm主要是一个C库,因此在加载某些数据后,必须使用自定义API函数来释放内存。这就是我的意思:structsvm_model*model;model=svm_load_model("pathtomodelfile");//dosomeprocessingsvm_free_and_destroy_model(&this->model);这些是我使用的libsvmAPI函数的定义:structsvm_model*svm_load_model(constchar*model_file_name);voidsvm_free_an

c++ - 动态分配数组的 unique_ptr 替代方案

我写了一个类,它必须与一些需要一些C风格数组(或至少指向第一个元素的指针)作为参数的旧代码接口(interface)。这些数组是我类的成员,它们特别大(50kb)所以我想把它们放在堆上,这样我类的对象在堆栈上就不会很大。我非常相信使用资源管理对象,所以我宁愿自己不在堆上管理这些数组。我发现使用unique_ptr的效果特别好。例如:std::unique_ptrsomeArrayName并使用:someArrayName(newSOMETYPE[someLargeSize])在我的构造函数的初始化列表中。这允许我使用.get()将它们用作常规C数组需要它作为参数的函数的方法,我不必自己

c++ - 为什么 GCC 给我一个错误 : no unique final overrider?

在下面的代码中,我收到以下警告和错误:test.cpp:15:warning:directbase'B'inaccessiblein'D'duetoambiguitytest.cpp:15:error:nouniquefinaloverriderfor'virtualvoidA::f()'in'D'但是如果我从A中移除B的虚拟继承(即structB:publicA),我只会得到警告,没有错误。structA{virtualvoidf()=0;};structB:publicvirtualA{voidf(){}};classC:publicB{};structD:publicC,virt

c++ - 使用 -g 选项编译但 "Single stepping until exit from function main, which has no line number information"

我在使用gdb时遇到了一些问题。这是我在一个名为main.cpp的文件中的代码#includevoidmyfunc();intmain(){charmsg[]="HelloWorld!";myfunc();std::cout我使用这个命令来编译这段代码:g++-g-Wallmain.cpp-ofoo接下来,我使用了gdb:$gdbfoo(gdb)startTemporarybreakpoint1at0x80487c3Startingprogram:/home/laptop/workspace/fooTemporarybreakpoint1,0x080487c3inmain()(gdb)

c++ - 为什么 list<unique_ptr> 中的 unique_ptr 的 std::move() 没有真正 move 它?

usingPtr=std::unique_ptr;Ptrf(boolarg){std::listlist;Ptrptr(newint(1));list.push_back(std::move(ptr));if(arg){Ptr&&obj1=std::move(list.front());//Here|obj1|and|list.front()|stillpointtothesamelocation!list.pop_front();returnstd::move(obj1);}else{Ptrobj2=std::move(list.front());list.pop_front();r

c++ - 为什么 boost::hash_combine 中的魔数(Magic Number)是十六进制指定的

本例中的魔数(MagicNumber)是0x9e3779b9,以10为基数是2654435769。代码有什么原因吗seed^=hash_value(v)+0x9e3779b9+(seed>2);使用十六进制表示而不是base-10表示?如果将代码中的0x9e3779b9替换为2654435769,功能是否会保持不变? 最佳答案 字面量就是字面量,同一字面量的不同表示形式……字面上相同。但是,表达式(文字或非文字)也有一个类型。等效的字面量应该是2654435769u(注意类型后缀使其成为unsigned)。看看这个简单的测试Live

c++ - 将 unique_ptr 的 vector 传递给对象。 vector 成为成员变量。正确的做法?

我将称之为“所有者”的一个对象在其生命周期内拥有数据对象vector的明确所有权。这些存储为unique_ptr的vector。一个对象/类,称为“Output”,需要在许多不同的方法中查看这些数据对象,因此某种引用/指针/变量是“Output”的成员变量.Output在其构造函数中接收数据对象的vector。我想到了三种方法来实现这一点。最好的方法是什么?选项1-“输出”对象将数据vec存储为常量引用:classOutput{//outputwantsthedata:public:Output(std::vector>const&in):my_lot_of_data(in){};st