草庐IT

shared_by_all

全部标签

C++ 如何在 shared_ptr vector 中存储多种类型?

如何在std::vector中存储多个shared_ptr,每个shared_ptr都带有指向不同类型的指针?std::vectorvec;vec.push_back(make_shared(3));vec.push_back(make_shared(3.14f));是否有一个基本的多态类,我可以将其用于该任务而无需使用特定于编译器的东西? 最佳答案 有几种方法可以做到这一点。我假设您想存储各种native类型,因为您正在使用int和float。如果您的类型列表是有限的,请使用boost::variant.例如std::vector

c++ - OpenMp 任务 : can't pass argument by reference

g++-fopenmpmain.cpp提示未定义对std::vector的引用。如何解决这个问题?我已经在Ubuntu上安装了libomp-dev包。主要.cpp#include#includetemplateTrecursiveSumBody(std::vector&vec){Tsum=0;#pragmaomptaskshared(sum){sum=recursiveSumBody(vec);}returnvec[0];}intmain(){std::vectora;recursiveSumBody(a);return0;}undefinedreference/tmp/ccTDECN

c++ - boost::shared_ptr 标准容器

假设我有一个foo类,并希望使用std::map来存储一些boost::shared_ptrs,例如:classfoo;typedefboost::shared_ptrfoo_sp;typededstd::mapfoo_sp_map;foo_sp_mapm;如果我向map添加一个新的foo_sp但使用的键已经存在,现有的条目是否会被删除?例如:foo_sp_mapm;voidfunc1(){foo_spp(newfoo);m[0]=p;}voidfunc2(){foo_spp2(newfoo);m[0]=p2;}原来的指针(p)被p2替换后会不会被释放?我很确定会这样,但我认为值得询问

c++ - 你应该如何用 shared_ptr 返回 *this?

另请参阅:Similarquestion下面的代码显然是危险的。问题是:您如何跟踪对*this的引用?usingnamespaceboost;//MyClassDefinitionclassMyClass{public:shared_ptrcreateOtherClass(){returnshared_ptrOtherClass(this);//baaad}MyClass();~MyClass();};//OtherClassDefinitionclassOtherClass{public:OtherClass(const*MyClassmyClass);~OtherClass();}

c++ - 使用值为 std::shared_ptr 的映射是否是具有多索引类列表的良好设计选择?

问题很简单:我们有一个类(class),成员有a,b,c,d...我们希望能够通过为a或b或c提供当前值来快速搜索(键是一个成员的值)并使用新值更新类列表...我想拥有一堆std::map>.1)这是个好主意吗?2)boostmultiindex是否在各个方面都优于这个手工制作的解决方案?出于简单性/性能方面的原因,PSSQL是不可能的。 最佳答案 BoostMultiIndex可能有一个明显的缺点,即它会尝试在集合的每次突变后使所有索引保持最新。如果您的数据加载阶段包含许多单独的写入,这可能会造成很大的性能损失。BoostMult

c++ - 将 shared_ptr 与多继承类一起使用

我有一个类继承了两个接口(interface):classMulti:publicIFoo,publicIBar{public:virtual~Multi();//FoopartvirtualvoidfooMethod();//...//BarpartvirtualvoidbarMethod();//...};不幸的是,这个类不能分解为每个接口(interface)的两个单独的类。事实上,在类实现中,这些实体(Foo和Bar)是紧密耦合的,但将来它们可能会分开。另一个类想要使用Multi类,有一个指向IFoo和IBar的指针:classClientClass{public:Client

c++ - 从 int 到 shared_ptr 的隐式转换

考虑下面的代码:#include#includevoidf(std::shared_ptrsp){}templateautocall_f(FuncTypef,PtrTypep)->decltype(f(p)){returnf(p);}intmain(){f(0);//doesn'tworkforanyotherint!=0,thanks@Rupesh//call_f(f,0);//error,cannotconvertinttoshared_ptr}在main()中的第一行,整数0转换为std::shared_ptr和电话f(0)成功没有任何问题。但是,使用模板调用函数会使情况有所不同

c++ - 通过 const reference 或 by value 传递 int,有什么区别吗?

这个问题在这里已经有了答案:Isitcounter-productivetopassprimitivetypesbyreference?[duplicate](7个答案)关闭7年前。当我将int和double之类的原语传递给函数时,是通过constreference传递它们更好,还是通过值传递它们更好(假设我不更改变量的值)?intgetValueFromArray(intindex){//returnthevaluefromthearray}intgetValueFromArray(constint&index){//returnthevaluefromthearray}谢谢

c++ - std::weak_ptr:锁或 shared_ptr 构造函数?

似乎有两种方法可以暂时获取weak_ptr指向的资源的所有权:使用lock()将weak_ptr传递给shared_ptr构造函数这两者都会产生一个shared_ptr,如果weak_ptr为空并且锁返回一个nullptrshared_ptr构造函数抛出异常。所以问题是:什么时候应该使用一个或另一个?是否有与此相关的一般准则或最佳做法? 最佳答案 复制自http://en.cppreference.com/w/cpp/memory/weak_ptr/lockBoththisfunctionandtheconstructorofstd

c++ - 警告 C4251 : needs to have dll-interface to be used by clients of class

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:std::vectorneedstohavedll-interfacetobeusedbyclientsofclass'Xwarning这是我在该组中的第一篇文章。我正在创建一个DLL并在应用程序的主文件中调用它。代码编译正常,但出现以下错误:warningC4251:'PNCBaseClass::m_vAvailChannelsFromRx':class'std::vector'needstohavedll-interfacetobeusedbyclientsofclass'PNCBaseClass'3>w