草庐IT

weak_ptr_cast

全部标签

c++ - void* 与 static_cast 对比 intptr_t 与 reinterpret_cast

我想知道两种不同类型的非常特殊类型的转换表之间是否存在特定的、基于标准的差异。特别是,给定:类型T和变量T*object是:intptr_topaque=reinterpret_cast(object);T*result=reinterpret_cast(opaque);相当于:void*opaque=static_cast(object);T*result=static_cast(opaque);我只关心result,它是否保证是相同的值,相当于任何类型T的原始object?我不在乎中间opaque有什么位模式,因为我相信标准技术上允许它们在每种情况下都不同(尽管没有理智的编译器会产

c++ - 为什么构造不完整类型的 std::unique_ptr 编译?

代码:#includestructData;std::unique_ptrmake_me();intmain(){std::unique_ptrm=make_me();return0;}当然失败了:Infileincludedfrom:1:Infileincludedfrom/opt/compiler-explorer/gcc-7.1.0/lib/gcc/x86_64-linux-gnu/7.1.0/../../../../include/c++/7.1.0/memory:80:/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bit

c++ - Unique_ptr 和前向声明

假设我有两个类(class):“Foo.h”#pragmaonceclassFoo{public:Foo(){};~Foo(){};};“啊”#pragmaonce#includeclassFoo;classA{public:A(){};~A(){};std::unique_ptrfoo;};A持有Foo的unique_ptr。我不想在“A.h”中包含Foo,所以我转发了它。通过在“A.h”中向前声明类Foo,我得到一个编译时错误:errorC2027:useofundefinedtype'Foo'errorC2338:can'tdeleteanincompletetype所以我关注了

c++ - 取消引用临时的 unique_ptr

unique_ptrmyFun(){unique_ptrpa(newA());returnpa;}constA&rA=*myFun();此代码可以编译,但rA包含垃圾。有人可以向我解释为什么这段代码无效吗?注意:如果我在取消引用之前将myFun的返回值分配给命名的unique_ptr变量,它可以正常工作。 最佳答案 unique_ptr会将所有权传递给另一个unique_ptr,但在您的代码中,没有任何内容可以从返回的指针中捕获所有权。换句话说,它不能转移所有权,所以它会被销毁。正确的做法是:unique_ptrrA=myFun()

c++ - std::shared_ptr 向上转换到基类 - 最好的方法?

哪种转换更好,有什么区别?classBase{};classDerived:publicBase,publicstd::enable_shared_from_this{};intmain(intargc,constchar*argv[]){std::shared_ptrptr1=std::dynamic_pointer_cast(std::shared_ptr(newDerived()));//version1std::shared_ptrptr2=std::shared_ptr(newDerived());//version2return0;} 最佳答案

c++ - 让 shared_ptr refs 出现在 doxygen 协作图中

我已经做了足够多的谷歌搜索知道如果我有什么喜欢classSubObject{public://blahblahblah};classAggregate{public:boost::shared_ptrm_ptr;};我可以让Doxygen创建“正确”的协作图如果我有一个像这样的虚拟声明namespaceboost{templateclassshared_ptr{T*dummy;};}在我的头文件中。我的问题是:我如何让它在我的所有项目中发挥作用以及我所有的标题,而不必实际包含该行在每个文件中? 最佳答案 呵呵....我觉得自己回答自

c++ - std::unique_ptr 带有用于 win32 LocalFree 的自定义删除器

我有win32APICommandLineToArgvW它返回一个LPWSTR*和警告我CommandLineToArgvWallocatesablockofcontiguousmemoryforpointerstotheargumentstrings,andfortheargumentstringsthemselves;thecallingapplicationmustfreethememoryusedbytheargumentlistwhenitisnolongerneeded.Tofreethememory,useasinglecalltotheLocalFreefunction

c++ - 使用boost程序选项时如何解决 "boost::bad_any_cast: failed conversion using boost::any_cast"?

//Usingboostprogramoptionstoreadcommandlineandconfigfiledata#includeusingnamespacestd;usingnamespaceboost;namespacepo=boost::program_options;intmain(intargc,char*argv[]){po::options_descriptionconfig("Configuration");config.add_options()("IPAddress,i","IPAddress")("Port,p","Port");po::variables_

c++ - 有什么理由使用 auto_ptr 吗?

在阅读Jossutis从他的STL书中对auto_ptr的解释后,我有一个强烈的印象,即无论我尝试在什么任务中使用它,我都会100%失败,因为auto_ptr的许多陷阱之一。我的问题是:在现实生活中是否有auto_ptr真正有用并且非常适合的任务? 最佳答案 很明显,auto_ptr输给unique_ptr.现在,在“没有增强的严格C++03”世界中,我使用auto_ptr很多时候,最值得注意的是:对于返回给定类型的动态分配实例的“工厂成员函数”:我喜欢使用std::auto_ptr在返回类型中明确指出对象必须被删除在尝试将对象插入

c++ - 将 auto_ptr<> 与数组一起使用

我正在使用auto_ptr它使用类指针类型的数组,那么我该如何为其赋值。例如auto_ptrarr[10];如何为arr赋值数组? 最佳答案 您不能将auto_ptr与数组一起使用,因为它调用deletep,而不是delete[]p。你想要boost::scoped_array或其他一些boost::smart_array:) 关于c++-将auto_ptr与数组一起使用,我们在StackOverflow上找到一个类似的问题: https://stackove