简体   繁体   English

OpenCV或Boost智能指针

[英]OpenCV or Boost smart pointers

I have an expanding image processing project which relies heavily on the OpenCV library for much of its functionality, although I do also use a few boost functions as well. 我有一个扩展的图像处理项目,它在很大程度上依赖于OpenCV库,尽管我也使用了一些boost函数。

I'd like to start using smart pointers to replace some raw pointers which are beginning to cause problems. 我想开始使用智能指针来替换一些开始引起问题的原始指针。 My question is on which type of smart pointer to use, with my main choices (I think) being the OpenCV cv::Ptr or one of the boost variants. 我的问题是关于使用哪种类型的智能指针,我的主要选择(我认为)是OpenCV cv::Ptr或其中一个boost变体。

I realise there are a number of questions explaining the different between each of the boost pointers, but I hoped somebody could offer an explanation of how cv::Ptr compares with them and make any recommendations of one or the other? 我意识到有很多 问题可以解释每个提升指针之间的差异,但我希望有人可以解释一下cv::Ptr如何与它们进行比较并提出其中一个推荐?

EDIT - I've noticed from the OpenCV docs that Ptr is similar to boost shared_ptr , is the essential difference just which library/include files are required? 编辑 - 我从OpenCV文档中注意到Ptr类似于boost shared_ptr ,是否需要哪些库/包含文件的本质区别?

For what I can see in OpenCV documentation, this is a reference-counted smart pointer, essentially, same as boost::shared_ptr . 对于我在OpenCV文档中可以看到的内容,这是一个引用计数的智能指针,基本上与boost::shared_ptr相同。 Even it uses atomic operations on the reference count. 甚至它在引用计数上使用原子操作。

I would make the choice based on portability and interoperability. 我会根据可移植性和互操作性做出选择。

  1. Is your system going to be ported elsewhere and depends on OpenCV for sure but not on boost ? 你的系统是否会被移植到其他地方并且肯定依赖于OpenCV而不是boost Then, stick to OpenCV cv::Ptr if you can avoid boost and you get rid of the dependency. 然后,坚持使用OpenCV cv::Ptr如果你可以避免提升,你就摆脱了依赖。

  2. Does boost::shared_ptr plays nice with the rest of OpenCV? boost::shared_ptr对OpenCV的其余部分有效吗? If you have something returning a cv::Ptr from OpenCV library, maybe it's better to stick to cv::Ptr in these cases, because the reference count will be handled incorrectly if you mix both kind of pointers and the resource could be destroyed prematurely. 如果你有一些东西从OpenCV库返回一个cv :: Ptr,也许在这些情况下坚持使用cv :: Ptr会更好,因为如果混合使用这两种指针并且资源可能会被过早销毁,则引用计数将被错误处理。

  3. You're going to stick to boost wherever you port the project? 无论你移植项目的哪个地方,你都会坚持不懈地boost Then, stick to boost::shared_ptr when you can do it, it's more standard, people know it and will immediately understand your code. 然后,当你能做到时,坚持使用boost::shared_ptr ,它更标准,人们都知道并且会立即理解你的代码。 UPDATE: In C++11 you have std::shared_ptr, which amounts to no dependency if you can afford it, so you can use std::shared_ptr in this case and get rid of boost also. 更新:在C ++ 11中你有std :: shared_ptr,如果你能负担得起它就没有依赖,所以在这种情况下你可以使用std :: shared_ptr并且也可以去掉boost。

Just as a side note, there is a technique to mix boost and std shared pointers that can keep the reference correctly around and could be useful for someone. 正如旁注,有一种技术可以混合使用boost和std共享指针,这些指针可以正确地保持引用,并且可以对某些人有用。 See this question, it could be relevant also for mixing other kind of reference-counted pointers: Conversion from boost::shared_ptr to std::shared_ptr? 看到这个问题,它可能也适用于混合其他类型的引用计数指针: 从boost :: shared_ptr转换为std :: shared_ptr?

In my experience, when you port something, the fewer dependencies, the better, or there are certain platforms for which compiling can be a hell. 根据我的经验,当您移植某些东西时,依赖性越少越好,或者某些平台的编译可能是一个地狱。 So make your choice based on portability if it's a concern and interoperability of pointers with libraries. 因此,如果它是指针与库的指针的关注点和互操作性,请根据可移植性做出选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM