草庐IT

weak_library

全部标签

c++ - weak_ptr C++ 中的比较运算符

我在新的STL成员中还是个新手。谁能指出为什么这段代码会出现段错误?#include#include#include#include#includeusingnamespacestd;structStubClass{weak_ptrb;intc;friendbooloperator==(StubClassx,StubClassy);friendbooloperatorx):b(x){c=5;}};booloperator==(StubClassd,StubClassc){returnd.b==c.b;}booloperatorspPtr(newstring("Hello"));weak

c++ - map 中过期的 weak_ptr 会发生什么

我想了解weak_ptr已过期的映射中的条目(类型为boost::weak_ptr)会发生什么。map中的相应条目是否会自动删除?键是一个整数,对应的值是一个weak_ptr。我写的示例代码,但无法编译#include#include#includeusingnamespacestd;classFoo:publicboost::enable_shared_from_this{public:Foo(intn=0):bar(n){std::coutinc_ref(){returnshared_from_this();}private:intbar;};std::map>mappy;intm

c++ - 如何使用 Boost Graph Library 更改图中的边权重?

我已经使用Boost图形库定义了一个图形,typedefboost::propertyEdgeWeightProperty;typedefboost::adjacency_listGraph;使用添加边相当简单boost::add_edge(vertice1,vertice2,weight,graph);我还没有弄清楚如何在设置边缘权重后更改它。一种可能的解决方案是删除边缘并使用更新后的权重值重新添加它,但是,这似乎有点过分。 最佳答案 一种解决方案是执行以下操作typedefboost::adjacency_listGraph;t

c++ weak_ptr在取消引用后过期?

我是智能指针的新手,我正在思考为什么weak_ptr在取消引用运算符后会过期。我用来测试的代码在这里:#include#include#includeusingnamespacestd;structnode{weak_ptrparent;shared_ptrchild;intval;};shared_ptrfoo(){shared_ptra=make_shared();shared_ptrb=make_shared();a->val=30;b->val=20;b->parent=a;a->child=b;returna;}intmain(){shared_ptrc=foo();node

c++ - 尝试理解 std::enable_shared_from_this<T> 但使用它会导致 bad_weak_ptr

我试图理解std::enable_shared_from_this类的行为,但我无法理解。所以我写了一个简单的程序来测试不同的情况。问题谁能解释一下下面代码的行为,因为我无法解释观察到的结果。谢谢你的帮助。代码#include#includestructC:std::enable_shared_from_this{};intmain(){{//test1std::shared_ptrfoo,bar;foo=std::make_shared();bar=foo->shared_from_this();//okstd::coutfoo=std::shared_ptr(newC);std::

c++ - 在库接口(interface)中提供对 weak_ptr 的访问是否明智?

我编写了一个库,公开了对多个相关对象类型的引用。所有这些对象的生命周期都由库在内部通过boost::shared_ptr管理。根据库的性质,库的用户还可以知道任何公开对象的生命周期。所以他们可以存储指针或保留对这些对象的引用。他们这样做并知道这些对象何时不再有效是合理的。但我对强制我的用户讲道理感到内疚。公开库是否可以接受weak_ptr的对象?其他图书馆有这样做吗?我已经分析了这个库在应用程序中的使用情况,发现它对任务至关重要,无法公开weak_ptr独家。让匹配的API函数公开一个引用或一个weak_ptr或使任何对象能够公开一个weak_ptr是否更明智?对自己?

c++ - 为什么我们需要 C++11 中的 weak_ptr?

我正在阅读NicolaiM.Josuttis的“TheC++StandardLibrary”一书以了解弱指针。作者提到了需要weak_ptr的两个原因,我不明白第二个原因。任何人都可以提供一个简单的解释以及以下原因的示例(引自书中):Anotherexampleoccurswhenyouexplicitlywanttosharebutnotownanobject.Thus,youhavethesemanticsthatthelifetimeofareferencetoanobjectoutlivestheobjectitrefersto.Here,shared_ptrswouldnev

c++ - Boost Graph Library astar和导航网格

我在做一个项目SFML/C++,我需要生成一个图来连接它们之间的障碍物以方便寻路,所以我有兴趣生成一个导航网格,我将应用boostA*算法。有点像这样:但是我在使用BoostGraphLibrary实现它时遇到了很多问题(如果您有一个更合适的库,我很感兴趣)。首先,我创建一个具有适当结构的adjacency_list:structWayPoint{sf::Vector2fpos;};structWayPointConnection{floatdist;};typedefboost::adjacency_listWayPointGraph;typedefWayPointGraph::ve

c++ - 使用 std::weak_ptr 和别名构造函数打破循环引用:听起来还是有问题?

我还没有在任何主要的C++论坛/博客(例如GotW)上找到以下打破循环引用的方法,所以我想问一下该技术是否已知,其优缺点是什么?classNode:publicstd::enable_shared_from_this{public:std::shared_ptrgetParent(){returnparent.lock();}//thegetterfunctionsensurethat"parent"alwaysstaysalive!std::shared_ptrgetLeft(){returnstd::shared_ptr(shared_from_this(),left.get())

c++ - 编译cuda文件报错: "runtime library" mismatch value 'MDd_DynamicDebug' doesn't match value 'MTd_StaticDebug' in vectorAddition_cuda. o

我尝试在Qt5.2和MSVC2012环境下编译一个cuda文件。在开始我的项目之前,我仔细阅读了问题并回复:CompilingCudacodeinQtCreatoronWindows.但是即使我简单地复制代码并在qt中生成2个文件,仍然会弹出一些错误/main.cpp/vectorAddition.cu错误是:errorLNK2038:mismatchdetectedfor'RuntimeLibrary':value'MDd_DynamicDebug'doesn'tmatchvalue'MTd_StaticDebug'invectorAddition_cuda.o我完全不知道如何修复这