boost::ptr_vector and find_if

我们两清 提交于 2019-12-02 11:37:17

The issue is that your equal method is not const qualified.

class Try
{
public:
  virtual ~Try();
  virtual bool equal(CMDatabase::TryPtr const& mySd) const = 0;
};

bool TryImpl::equal(CMDatabase::TryPtr const& mySd) const { return true; }

Note:

  • the const added to the method, otherwise it cannot be used on const objects
  • the const added on the pointer: copying shared_ptr does cost, because it necessitates incremented a shared counter, and decrementing it afterward.

EDIT:

Reminder for the unwary: the Pointer Container library has been designed so that the interface would be as easy to use a possible, and one of the goody is that you don't have to double dereference. This compiles:

 boost::ptr_vector<int> vec;
 vec.push_back(new int(3));

 int& i = *vec.begin();

Thus, your functor must take a reference, not a pointer :)

Just for sake of completeness, the following statement is wrong! Thanks to Matthieu M. to point out my mistake!

On dereferencing an iterator of the boost pointer container you will get the pure pointer to the element. So you can try to dereference the pure pointer you get through the iterator:

CMDatabase::TryVectorIterator it =
  find_if(updateTry.begin(), updateTry.end(), bind1st(mem_fun(&CMDatabase::Try::equal), **i));

Where the following is still correct ;)

Or you can use the the operator[] implementation of the boost::ptr_vector which will return a reference to the element:

for (std::size_t i = 0, l = ; defaultTry.size(); ++i) {
  CMDatabase::TryVectorIterator it = std::find_if(
    updateTry.begin(),
    updateTry.end(),
    std::bind1st(std::mem_fun(&CMDatabase::Try::equal), defaultTry[i])
  );
}

Hope this helps.

Sorry, For soem reason, i could not add comment to previous post, so I am writing it as answer.

I tried both the methods. First one gives, illegal indirection.

And for the second one, it gives the same error: In verbose output, the details are as follows:

c:\program files\microsoft visual studio 9.0\vc\include\functional(296) : error C2440: 'initializing' : cannot convert from 'const CMDatabase::Try' to 'CMDatabase::Try *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called c:\fta_chk\tools\channel_editor\ivodb\channellistupdate.cpp(103) : see reference to function template instantiation 'std::binder1st<_Fn2> std::bind1st,CMDatabase::Try>(const _Fn2 &,const _Ty &)' being compiled with [ _Fn2=std::mem_fun1_t, _Result=bool, _Ty=CMDatabase::Try, _Arg=CMDatabase::TryPtr ]

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!