草庐IT

File_ptr

全部标签

c++ - `weak_ptr` 和 `shared_ptr` 访问如何是原子的

std::shared_ptrint_ptr;intmain(){int_ptr=std::make_shared(1);std::threadth{[&](){std::weak_ptrint_ptr_weak=int_ptr;autoint_ptr_local=int_ptr_weak.lock();if(int_ptr_local){cout上面的代码线程安全吗?我读了这个答案Aboutthread-safetyofweak_ptr但只是想确保上面的代码是线程安全的。我问这个的原因是,如果上面的代码确实是线程安全的,我无法理解std::weak_ptr是如何实现的。和std::s

c++ - 定义一个类的私有(private)整型常量 : in the header or in the cpp file?

主题主要在此处解决(Wheretodeclare/defineclassscopeconstantsinC++?)特别是here.我想完全理解的是,在积分常数的情况下,它们之间有什么区别://IntheheaderclassA{private:staticconstintmember=0;//Declarationanddefinition};和://IntheheaderclassA{private:staticconstintmember;//Onlydeclaration};//InthecppconstintA::member=0;//Definition(据我所知,第二种可能

c++ - 通过采用 void * 的 C 接口(interface)传递 shared_ptr

我有一个使用SDL的C++项目,特别是SDL事件。我想将事件系统用于传入的网络消息,就像它用于UI事件一样。我可以定义一个新的事件类型并附加一些任意数据(参见thisexample)。如果我使用普通指针,这就是我会做的:Uint32message_event_type=SDL_RegisterEvents(1);/*Inthemaineventloop*/while(SDL_Poll(&evt)){if(evt.type==message_event_type){Message*msg=evt.user.data1;handle_message(msg);}}/*Networkingc

c++ - "Use of undefined type"带有 unique_ptr 以转发声明的类和默认移动构造函数/赋值

在下面的代码中,是避免编译错误并在A.cpp中手动包含B.h实现移动构造函数/赋值的唯一方法吗?//A.h#includeclassB;//implementationsomewhereinB.h/B.cppclassA{public:A()=default;~A()=default;A(constA&)=delete;A&operator=(constA&)=delete;A(A&&)=default;A&operator=(A&&)=default;private:std::unique_ptrm_b;};VisualStudio2015给出“错误C2027:使用未定义类型”,因为

c++ - 如何比较 time_t 和 std::filesystem::file_time_type

我正在将一些代码从boost::filesystem转换到std::filesystem。以前使用的代码boost::filesystem::last_write_time()它返回一个time_t,因此直接与我已经持有的time_t对象进行比较是微不足道的。顺便说一句,我持有的这个time_t是从很久以前保存的文件内容中读取的,所以我坚持使用这种“自unix纪元以来的时间”类型。std::filesystem::last_write_time返回std::filesystem::file_time_type.是否有可移植的方法将file_time_type转换为time_t,或者以其

c++ - 使用自定义 std::ostream 包装 FILE*

我有一个与std::ostream一起工作的函数。我需要支持使用C文件句柄(FILE*)。我应该创建我自己的std::ostream的子类来委托(delegate)给FILE*吗? 最佳答案 正如BenVoigt所指出的,您想要子类化streambuf。南加州大学网站上的某些页面有documentation,header,和source对于包装FILE*的streambuf子类(stdiobuf)的GNU实现。它对作为(GroovX)一部分的库有一些依赖性,但这些应该很容易删除(我将从删除对GVX_TRACE的所有引用开始)。有趣的

如何使file_get_contents返回唯一结果

我是PHP的新手,想向您寻求帮助,以返回File_get_contents()的唯一结果。原因是我想给每张照片一个唯一的名称,因此以后可以删除其中一个,而不是全部。$file=addslashes(file_get_contents($_FILES['image']['tmp_name'][$key]));不幸的是,Time()和Microtime()在这种情况下无济于事。看答案也许这会帮助您:http://php.net/manual/en/function.uniqid.phpuniqid();$ImageName=$ImageName。'_'。uniqid();

c++ - std::unique_ptr 删除函数,initializer_list - 驱动分配

全部,当我使用初始化列表格式实例化小部件数组时,指向成员变量小部件实例的裸指针可以编译,但在更改为std::unique_ptr后,gcc会给出有关已删除函数的编译错误。$uname-aLinux..3.5.0-21-generic#32-UbuntuSMP2012年12月11日星期二18:51:59UTCx86_64x86_64x86_64GNU/Linux$g++--versiong++(Ubuntu/Linaro4.7.2-5ubuntu1)4.7.2此代码给出以下编译器错误:#include#includeclassWidget{public:Widget(){}};class

c++ - 为什么 observer_ptr 在 move 后不归零?

为什么不是observer_ptrmove操作后归零?它在默认构造中被正确设置为nullptr,这确实有意义(并防止指向垃圾)。相应地,当std::move()开始时,它应该被清零,就像std::string、std::vector等这将使它成为多个上下文中的一个很好的候选者,在这些上下文中,原始指针是有意义的,并且自动在具有原始指针数据成员的类上生成move操作,例如thiscase.编辑正如@JonathanWakely在评论中指出的(与aforementionedquestion相关):ifobserver_ptrwasnullafteramoveitcanbeusedtoimp

c++ - 通过取消引用复制 std::unique_ptr 的值

我写了下面的代码,我试图将unique_ptr对象的值复制到一个结构中。#include#includeusingnamespacestd;structS{S(intX=0,intY=0):x(X),y(Y){}//S(constS&){}//S&operator=(constS&){return*this;}intx;inty;std::unique_ptrptr;};intmain(){Ss;s.ptr=std::unique_ptr(newS(1,4));Sp=*s.ptr;//Copythepointer'svaluereturn0;}它在VisualC++2012中弹出错误: