草庐IT

together_unique

全部标签

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

c++ - boost 序列化 std::unique_ptr 支持

boost序列化库支持std::unique_ptr的序列化吗?我试图编译下面的代码,但如果我包含boost::archive::text_oarchiveoa(ofs);oa行,编译器(顺便说一句,带有-std=c++11标志的gcc4.7)抛出一个错误/usr/include/boost/serialization/access.hpp:118:9:错误:‘classstd::unique_ptr’没有名为‘serialize’的成员#include#include#include#include#includeclassMyDegrees{public:voidsetDeg(in

c++ - 尽管使用 unique_ptr 内存泄漏

我有一个类的构造函数,它用传递给它的值初始化该类内部的unique_ptr。出于某种原因,valgrind提示内存泄漏:22,080(24direct,22,056indirect)bytesin1blocksaredefinitelylostinlossrecord6of6at0x4C2C7A7:operatornew(unsignedlong)(in/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)by0x4A64FB:VectorBasedNodeOrder::VectorBasedNodeOrder(VectorBasedN

c++ - 在 map 中使用 unique_ptr 时删除 std::pair 中的函数

我有一段C++代码,我不确定它是否正确。考虑以下代码。#include#include#includeusingnamespacestd;intmain(intargc,char*argv[]){vector>>v;v.resize(5);returnEXIT_SUCCESS;}GCC编译这段代码没有问题。然而,英特尔编译器(版本19)因错误而停止:/usr/local/[...]/include/c++/7.3.0/ext/new_allocator.h(136):error:function"std::pair::pair(conststd::pair&)[with_T1=cons

c++ - `unique_ptr` 上的原子操作

std::shared_ptrhasspecializationsforatomicoperations像atomic_compare_exchange_weak和family,但我找不到关于std::unique_ptr的等效特化的文档。有没有?如果不是,为什么不呢? 最佳答案 可以提供std::shared_ptr的原子实例的原因并且不可能为std::unique_ptr这样做在他们的签名中暗示。比较:std::shared_ptr对比std::unique_ptr其中D是删除器的类型。std::shared_ptr需要分配一个

c++ - std::move 和 unique_ptr::reset 之间有什么区别?

对于std::unique_ptr的p1和p2,std::move()有什么区别>和std::unique_ptr::reset()?p1=std::move(p2);p1.reset(p2.release()); 最佳答案 根据[unique.ptr.single.assign]/2中移动分配的标准规范,答案应该是显而易见的:Effects:Transfersownershipfromuto*thisasifbycallingreset(u.release())followedbyanassignmentfromstd::forw

c++ - std::bind 通常与仅移动类型一起使用,特别是 std::unique_ptr 吗?

我正在尝试使用boost::asio并遇到了一些问题。我正在尝试编译以下代码:std::unique_ptrbuffer=buffers.pop();std::functiont=std::bind(&tcp_client::handle_read_done,this,std::placeholders::_1,std::placeholders::_2,std::move(buffer));如果我排除std::move(buffer),一切正常,当然是从handle_read_done的签名和作为std::bind中传递的参数。当试图将它传递给boost::asio::async_r