草庐IT

c++ - std::shared_ptr 预分配内存

我想在一个堆请求(如std::make_shared)中为shared_ptr的控制block和value_type预分配内存,但是不要立即在其中构造任何对象。当我实际需要构造对象时,使用放置new。可能吗?std::make_shared或std::allocate_shared似乎都无法解决我的问题。 最佳答案 我建议创建延迟初始化包装类,它本身包含足够的内存供您稍后要初始化的对象使用。这个包装器甚至可以有特殊的方法来在析构函数中调用placementnew和delete初始化对象。

c++ - 具有类型删除析构函数的 unique_ptr 不太有效(有警告)

有一个不错的小技巧here允许使用std::unique_ptr不完整的类型。相关代码如下://File:erasedptr.h#include#include//typeeraseddeletor(animplementationtypeusing"veneer")templatestructErasedDeleter:std::function{ErasedDeleter():std::function([](T*p){deletep;}){}};//Aunique_ptrtypedeftemplateusingErasedPtr=std::unique_ptr>;//Declar

c++ - CMake : softlink resource ( such as GLSL shaders ) or copy each complilation

使用CMake将资源从源目录复制到构建目录的最简单方法是file(COPY${CMAKE_CURRENT_SOURCE_DIR}/resourcesDESTINATION${CMAKE_CURRENT_BINARY_DIR})但是,这仅当我调用cmake时才会更新构建目录中的资源。我需要每次调用make来更新资源。例如现在我开发了一些GLSL着色器。我需要同时更改C++代码和GLSL代码,并且我需要在我的IDE中每次点击compile或run时一切都同步(我将CodeBlocks与CMake生成的项目文件一起使用)简单的解决方案是使从源目录到构建目录的软链接(softlink)。但我不

c++ - 如何从以 shared_ptr 为键的 unordered_set 中获取基于 raw_ptr 的元素

我想知道是否有办法根据unordered_set的原始指针检索一个元素,该元素以shared_ptr为键。unordered_set>sets;automyobj=make_shared();sets.insert(myobj);//Findtheelementmyobjsets.find(myobj);//Howtofindtheelementbasedontheunderlyingrawpointer?sets.find(my.obj.get());(my.obj.get())); 最佳答案 要仅使用底层原始指针查找您要查找的内

c++ - Boost.asio 和异步链,unique_ptr?

我对异步编程不是很熟悉,我有一个问题。我的问题如下。给定boost.asio中C++11的echo_server示例:http://www.boost.org/doc/libs/1_60_0/doc/html/boost_asio/example/cpp11/spawn/echo_server.cpp我想知道std::make_shared可以在C++14中替换为std::unique_ptr在C++14中,避免了引用计数的开销。我不确定,因为我们有shared_from_this()但不是像unique_from_this()这样的东西,那么我怎样才能访问unique_ptr从里面t

c++ - [conv]/6中语句 "The expression e is used as a glvalue if and only if the initialization uses it as a glvalue"的确切含义是什么

[conv]/6(重点是我的):Theeffectofanyimplicitconversionisthesameasperformingthecorrespondingdeclarationandinitializationandthenusingthetemporaryvariableastheresultoftheconversion.TheresultisanlvalueifTisanlvaluereferencetypeoranrvaluereferencetofunctiontype([dcl.ref]),anxvalueifTisanrvaluereferencetoob

c++ - 让 shared_ptr 成员破坏 CopyConstructible 契约吗?

我刚刚在codereview争论过其中声明具有std::shared_ptr成员的类会破坏CopyConstructible契约(Contract),特别是:ThefollowingexpressionsmustbevalidandhavetheirspecifiedeffectsTu=v;Thevalueofvisunchanged原因是复制会通过增加shared_ptr的引用计数来更改源对象,但我的反对意见是引用计数与shared_ptr。更改引用计数是一种副作用,但引用并未说明禁止在被复制的对象之外产生副作用。但我不是语言律师,所以我可能是错的。根据C++标准,什么是正确的?

c++ - as_bytes 函数的精确定义

我在阅读时发现了这个函数,但我在CPPreference上找不到它的定义|programmingPrinciplesbyBjarnestroustrup它的用法是这样的:ifs.read(as_bytes(x),sizeof(int));`我了解read的工作原理,但您仍然可以帮助我了解to_bytes标准定义。 最佳答案 as_bytes函数返回参数第一个字节的地址(因此read调用将覆盖对象x)。因此,在C++11或更高版本中,可以按如下方式编写该函数:templatechar*as_bytes(T&x){return&rein

使用 unique_ptr 包含 char 数组的 C++ 对象

我正在寻找一种方法来使用unique_ptr来分配一个结构,该结构包含一个char数组,其中包含动态设置的字节数以支持不同类型的消息。假设:structMyMessage{uint32_tid;uint32_tdata_size;chardata[4];};如何将下面的send_message()转换为使用智能指针?voidsend_message(void*data,constsize_tdata_size){constautomessage_size=sizeof(MyMessage)-4+data_size;constautomsg=reinterpret_cast(newcha

c++ - unique_ptr 作为模板参数

为什么要在VS2017中编译?#include#includeusingnamespacestd;structx{x(){coutvoidfoo(T&&item){structboo{Titem;boo(T&&t):item(std::move(t)){}};newboo(std::move(item));}intmain(){std::unique_ptrb(newx);foo(b);//IwouldexpectthatIshouldputstd::move(b)here.}按照编写的代码,输出是x()~x()如果foo(b)行写为foo(std::move(b)),那么输出就是x(