草庐IT

base_ptr

全部标签

c++ - 如何将 std::unique_ptr<T> 与返回 int 的接口(interface)一起使用?

我想将open/closePOSIXAPI包装到一个RAII兼容对象中,例如std::unique_ptr。但是open函数返回一个int(即不是HANDLE,它是指向void的指针),并且我不确定如何将std::unique_ptr模板类与int一起使用。有人可以帮帮我吗? 最佳答案 真的,您想要的只是让close(intfileHandle)为您管理,对吗?为什么不创建一个带有为您调用close()的析构函数的简单C++类?我认为这就是您要寻找的行为。std::shared_ptr,friend只处理用new创建的堆指针,会调用

c++ - 如何深入研究 shared_ptr [Netbeans、clang++、gdb]

我正在使用NetbeansC++8.0.2clang++(Ubuntuclang版本3.6.0-2ubuntu1(tags/RELEASE_360/final)(基于LLVM3.6.0))gdb(GNUgdb(Ubuntu7.9-1ubuntu1)7.9)在我的“C++简单测试”中,每当我检查一个shared_ptr变量时,我看到的所有值都是:std::shared_ptr(count1,weak0)0x64d3a0或类似的。无法深入了解它实际指向的值。即使变量窗口中的TreeView显示了其中一个扩展器图标,当我单击它时它也会消失。当我尝试取消引用它或在“表达式”窗口中调用它的get

c++ - 防止对 std::unique_ptr 的不安全取消引用

摘自cppcon2015的幻灯片:unique_ptrf(){autoa=make_unique();returna;}//Whydoesthisevencompile?constA&dangling=*f();//BOOM!!!use(dangling);我的问题是:对于*this的右值引用,这可以解决吗?我在cppreference的规范中看到:typenamestd::add_lvalue_reference::typeoperator*()const;问题:不允许operator*用于右值unique_ptr并且只对左值unique_ptr取消引用有效吗?仍然有有效的用例来保持

vue3管理系统中后台返回pdf格式的文件流,前端如何预览?以及uniapp微信小程序中后台返回的base64位的pdf文件如何预览?

vue31.先看一下后台接口返回的文件流。2.安装插件,在使用的页面引入并使用//1安装插件npmivue3-pdf-app//2在页面中引入并使用importVuePdfAppfrom"vue3-pdf-app";import"vue3-pdf-app/dist/icons/main.css";//使用组件vue-pdf-appstyle="height:100vh;":pdf="state.pdfImg"/>3.修改请求返回的数据类型responseType:'blob'exportfunctionGetColoscopyPdf(id:any){returnhttpRequest({ u

c++ - 如何使用 Cereal 序列化 boost::ptr_vector?

是否可以使用cereal序列化boost::ptr_vector实例?如果是,怎么办? 最佳答案 绝对有可能。您可以在存档和指针类型上创建外部save()和load()模板函数,如下所示:#include#include#include#include#include//Sampleserializableobject.structMyRecord{std::strings_;MyRecord(conststd::strings=std::string()):s_(s){}templatevoidserialize(Archive&

c++ - 使用 std::weak_ptr 和别名构造函数打破循环引用:听起来还是有问题?

我还没有在任何主要的C++论坛/博客(例如GotW)上找到以下打破循环引用的方法,所以我想问一下该技术是否已知,其优缺点是什么?classNode:publicstd::enable_shared_from_this{public:std::shared_ptrgetParent(){returnparent.lock();}//thegetterfunctionsensurethat"parent"alwaysstaysalive!std::shared_ptrgetLeft(){returnstd::shared_ptr(shared_from_this(),left.get())

c++ - 为用户定义类型的 shared_ptr 专门化标准库函数是否合法?

标准说明了以下关于从标准库中专门化模板的内容(通过Whatcanandcan'tIspecializeinthestdnamespace?)Aprogrammayaddatemplatespecializationforanystandardlibrarytemplatetonamespacestdonlyifthedeclarationdependsonauser-definedtypeandthespecializationmeetsthestandardlibraryrequirementsfortheoriginaltemplateandisnotexplicitlyprohi

c++ - 为什么这段代码会出现 "exception spec is more lax than base"错误?

尝试使用Xcode6.1中的clang版本(基于LLVM3.5svn的clang-600.0.54)编译以下代码,使用-std=c++11和-stdlib=libc++给我一些我不太明白的错误。#includestructImpl{typedefstd::functionL;Ll;inti;};structHndl{Impl*impl;Hndl(Impl*i):impl(i){}~Hndl()noexcept(false){}};intmain(intargc,char*argv[]){Hndlh(newImpl());h.impl->l=[=]{h.impl->i=42;};retu

带有 shared_ptr 的 C++ openmp

这是一个困扰我的最小例子#include#include#include"omp.h"classA{public:A(){std::coutsim(std::make_shared());}for(unsignedinti=0;isim(std::make_shared());}}如果我多次运行这段代码,我可能会得到这样的结果:0xea33080xea32d80xea33380x7f39f80008c80xea33380xea33380xea33380xea3338我意识到最后4个输出的数量总是相同的字符(8)。但由于某种原因,它发生(不总是)一个或多个第四个输出包含更多(14)个字符

C++11 将 shared_ptr 转换为 vector 和类

我正在尝试将转换应用到shared_ptr并存储到shared_ptr,同时还在类中使用函数。我创建了这个例子:#include#include#include#includeusingnamespacestd;classMyClass{public:intfactor=0;MyClass(constintfactor_):factor(factor_){}shared_ptr>mult(shared_ptr>numbers){shared_ptr>result(newvector());transform(numbers->begin(),numbers->end(),result-