草庐IT

weak_ptr_cast

全部标签

c++ - std::list<std::unique_ptr>:空初始化列表与默认构造函数

代码#include#includeclassB;classA{std::list>bs;public:A();~A();};intmain(){Ax;return0;}显然编译。它没有链接,因为A::A()和A::~A()丢失了,但这是预料之中的。改变std::list>bs;应该调用std::list的标准构造函数list():list(Allocator()){}(C++14及以上)到std::list>bs{};应该调用list(std::initializer_list,constAllocator&=Allocator());默认构造函数也是。(感谢NicolBolas,他

c++ - 在未对齐的内存上重新解释_cast

假设以下代码:structA{inta;intb;};char*buffer=receivedFromSomeWhere();Aa=*reinterpret_cast(buffer+1);如果buffer+0在int的大小上对齐,buffer+1很可能在未对齐的内存上。默认复制构造函数可能会愉快地复制两个未对齐的int成员a和b。在x86/x64架构上,除了减慢代码速度外,它是否会以任何讨厌的方式影响a的复制构造?我知道一个好的序列化可以解决未对齐的内存问题(通过在某处添加一个填充以使A结构在buffer中对齐),但就我而言,我不对这部分负责。 最佳答案

c++ - 当一个方法只接受 Foo *const 时,我应该 const_cast "this"吗?

我有一个类Foo这是一个self引用的树状结构(最低限度):classFoo{public://Getsthischild'spositionrelativetoit'sparent.intgetPosition()const{returnparent->indexOf(this);}intindexOf(constFoo*constchild)const{returnchildren.indexOf(child);//thislinecausesanerror.}private:Foo*parent;QListchildren;}行returnchildren.indexOf(chi

c++ - 如果我碰巧注意到它已过期,我应该在 weak_ptr 上调用 reset 吗?

我收藏了Creature使用std::make_shared在我的应用程序的一部分中创建和拥有的对象和std::shared_ptr.我还跟踪了零个或一个的选择Creature在World使用std::weak_ptr的对象.voidWorld::SetSelection(conststd::shared_ptr&creature){selection=creature;}std::shared_ptrWorld::GetSelection()const{returnselection.lock();}GetSelection的来电者负责检查指针是否为空。如果是,则表示当前没有选择。T

c++ - 将 std::unique_ptr<void> 与自定义删除器一起用作智能 void*

我有一个通用类myClass有时需要根据用途存储额外的状态信息。这通常是通过void*完成的。,但我想知道我是否可以使用std::unique_ptr以便在类实例被析构时自动释放内存。问题是我需要使用自定义删除器,因为删除void*会导致未定义的行为。有没有办法默认构造一个std::unique_ptr,这样我就不用先用一个虚拟删除器构造它,然后在我使用void*时设置一个真正的删除器。对于状态结构?或者是否有更好的方法在类中存储状态信息?下面是一些示例代码:voiddummy_deleter(void*){}classmyClass{public:myClass():m_extraD

c++ - 如果过期的 weak_ptr 会给出未定义的行为,那么 owner_less 有什么意义呢?

请顾及我的经验不足,不明白std::owner_less的意义.我已经shown那一个map与weak_ptr不推荐作为key,因为已过期weak_ptrkey会破坏map,实际上:Ifitexpires,thenthecontainer'sorderisbroken,andtryingtousethecontainerafterwardswillgiveundefinedbehaviour.这种行为有多不确定?我问的原因是因为docs说说owner_less:Thisfunctionobjectprovidesowner-based(asopposedtovalue-based)mi

c++ - clang 静态分析器是否因从 unique_ptr 列表中弹出前面而感到困惑?

以下C++11代码是我认为会在clang中触发误报的最小示例:#include#include#includeclassElementType{};intmain(intargc,constchar*argv[]){std::list>theList(5);theList.pop_front();for(constauto&element:theList){//(*)std::cout在标有星号(*)的行上,clang分析器声明...filePath.../main.cpp:21:29:Useofmemoryafteritisfreed(withinacallto'begin')就我的

c++ - 转换函数是否返回 void "old-style-cast"?

Coverity提示我们代码库中的各种函数调用没有检查返回值。Uncheckedreturnvalue(CHECKED_RETURN)3.check_return:CallingAppendwithoutcheckingreturnvalue(asisdoneelsewhere73outof78times).在过去,我们会通过将返回值转换为void(如讨论的here)来简单地解决这个问题(在仔细检查返回值确实不重要之后):(void)Foo.Append(bar);但是,我们正朝着启用所有警告的方向努力,并且将警告视为错误,所以我有点担心上面的代码会生成一个old-style-cast

c++ - C++ Cast的实现

我正在浏览CodeProject中的一些代码,发现了以下用于C++转换的代码。templateunionhorrible_union{OutputClassout;InputClassin;};templateinlineOutputClasshorrible_cast(constInputClassinput){horrible_unionu;u.in=input;returnu.out;}为什么要按上面的方式实现cast。为什么我们不能只进行手动转换。有人可以举例说明正常类型转换何时不起作用吗? 最佳答案 尽管这种方法依赖于未定

C++11 move(x) 实际上意味着 static_cast<X&&>(x)?

这个问题在这里已经有了答案:Whenisthemoveconstructorcalledinthe`std::move()`function?(2个答案)关闭9年前。刚刚阅读了Stroustrup的C++编程语言第4版,在第7章中他说:move(x)meansstatic_cast(x)whereXisthetypeofx和Sincemove(x)doesnotmovex(itsimplyproducesanrvaluereferencetox)itwouldhavebeenbetterifmove()hadbeencalledrval()我的问题是,如果move()只是将变量转换为r