草庐IT

weak_ptr_cast

全部标签

c++ - 为什么捕获 std::bad_cast 在 FreeBSD 9 上不起作用?

考虑这段代码(badcast.cpp):#include#include#includeclassfoo{public:virtual~foo(){}};classbar:publicfoo{public:intval;bar():val(123){}};staticvoidcast_test(constfoo&f){try{constbar&b=dynamic_cast(f);printf("%d\n",b.val);}catch(conststd::bad_cast&){printf("badcast\n");}}intmain(){foof;cast_test(f);return

c++ - g++ 上 constexpr 上下文中成员指针的 static_cast

我在使用static_cast在constexpr上下文中向上转换成员指针时遇到了g++问题。请参见代码示例。在使用g++6.3和7.0版进行编译时,会出现编译错误,指出reinterpret_cast不是常量表达式。虽然clang4.0版没有给出错误,但我认为这是正确的,因为这里没有reinterpret_cast。这是g++或clang中的错误吗?什么是正确的行为?structBase{};structDerived:Base{inti;};structPtr{constexprPtr(intDerived::*p):p(static_cast(p)){}intBase::*p;}

c++ - 将 reinterpret_cast 的派生类指针转换为基类指针未定义行为吗?

看一个简单的例子:structBase{/*somevirtualfunctionshere*/};structA:Base{/*members,overriddenvirtualfunctions*/};structB:Base{/*members,overriddenvirtualfunctions*/};voidfn(){Aa;Base*base=&a;B*b=reinterpret_cast(base);Base*x=b;//usexhere,callvirtualfunctionsonit}这个小片段是否有未定义的行为?reinterpret_cast定义良好,它返回base

c++ - 创建一个非线程安全的 shared_ptr

我正在开发一个多线程程序,但有一个UI组件广泛使用std::shared_ptr来管理元素。我可以保证只有一个线程会使用这些shared_ptrs。有没有一种方法可以定义一个不会产生线程安全引用计数开销的shared_ptr?它可以基于boost::shared_ptr或std::shared_ptr。编辑:感谢提到intrusive_ptr的回答。我忘了提到我还需要weak_ptr功能,所以排除了它。更新:我的答案是使用Boost中的local_shared_ptr。查看来自“他漫步”的评论 最佳答案 AndreiAlexandr

c++ - 锁定 shared_ptr

我有一个共享对象需要发送到系统API并稍后将其提取回来。系统API仅接收void*。我不能使用shared_ptr::get()因为它不会增加引用计数,并且它可能在从系统API提取之前被其他线程释放。发送一个新的shared_ptr*将起作用,但涉及额外的堆分配。一种方法是让对象派生自enable_shared_from_this。但是,由于此类模板仅拥有一个weak_ptr,因此不足以防止对象被释放。所以我的解决方案如下所示:classMyClass:publicenable_shared_from_this{private:shared_ptrm_this;public:void*

c++ - 多个 shared_ptr 存储相同的指针

考虑这个程序:#include#includeclassX:publicstd::enable_shared_from_this{public:structCleanup1{voidoperator()(X*)const;};structCleanup2{voidoperator()(X*)const;};std::shared_ptrlock1();std::shared_ptrlock2();};std::shared_ptrX::lock1(){std::cout(this,Cleanup1());}std::shared_ptrX::lock2(){std::cout(this

c++ - 为什么 unique_ptr 不能推断删除器的类型?

假设我想使用带有unique_ptr的自定义删除器:voidcustom_deleter(int*obj){deleteobj;}为什么我要这样写:std::unique_ptrx(newint,custom_deleter);而不是这个:std::unique_ptrx(newint,custom_deleter);//doesnotcompile?不能推断删除器的类型吗? 最佳答案 对于unique_ptr,删除器是类型的一部分:template>classunique_ptr;因此,当您构造一个对象时,您需要指定它的类型。你正

c++ - 如何在 DDD(或 gdb)中使用 unique_ptr 调试 C++11 代码?

std::unique_ptr很好,但我发现在DDD中调试时不太舒服或gdb.我正在使用作为gcc一部分的gdbpretty-print(例如,/usr/share/gcc-4.8.2/python/libstdcxx/v6/printers.py)。这是可读性的一大胜利,例如:$printpTeststd::unique_ptrcontaining0x2cef0a0但是,取消引用指针不起作用:$print*pTestCouldnotfindoperator*.当我需要访问该值时,我必须手动复制指针并将其转换为正确的类型,例如:print*((MyType*)0x2cef0a0)如果进

c++ - 将 reinterpret_cast 输入重新解释为 std::unique_ptr 永远不会真正安全吗?

当使用具有可变大小结构(必须分配为byte[]然后转换为结构)的各种API时,如果unique_ptr持有者可以指向该结构,那将是很好的,因为这就是我们将要做的正在使用。例子:std::unique_ptrv;v.reset(reinterpret_cast(newBYTE[bytesRequired]));这允许`v提供结构本身的View,这是更可取的,因为我们不需要第二个变量,除了删除之外我们不关心字节指针。问题在于可能会在强制转换上对指针进行thunk(使其释放不安全)。我看不出为什么编译器会在cast上更改指针值(因为没有继承),但我听说标准保留对任何cast上的任何指针进行t

c++ - 使用别名模板时无法将 `std::unique_ptr` 分配给 clang 中的基类

以下代码在gcc4.9.3和clang3.7.1上编译和运行得很好//std::unique_ptr#include//Templateclassfortemplate-templateargumentstemplatestructBar{};//BaseclasstemplateclassXX>structBase{};//DerivedclassthatoperatesonlyonBartemplatestructDerived:publicBase{};//Holdstheunique_ptrtemplateclassXX>structFoo{std::unique_ptr>fo