Boost binding question

I am trying to compare strings (char*) using strcmp but I am having a hard time doing it with boost::bind. It compiles but it crashes when I run it.

I have a std::vector<boost::shared_ptr<DeviceInfo>> cMonitoredDevices and one cCurrentDevices. I used a typedef DeviceContainer for std::vector<boost::shared_ptr<DeviceInfo>>.

DeviceInfo is a simply struct that contains a char[128] Name (and other fields not important for this issue) that I want to use to compare.

So I am trying to find the DeviceInfo (based on Name) that are in cMonitoredDevice but not in cCurrentDevices. My problem is retrieving the Name value for the strcmp. Here is what I have so far

1
2
3
4
5
for(DeviceContainer::iterator pDevice = m_cMonitoredDevices.begin(); pDevice != m_cMonitoredDevices.end(); pDevice++) {
   if (std::find_if(cCurrentDevices.begin(), cCurrentDevices.end(),
      boost::bind(&strcmp, boost::bind(&boost::shared_ptr<DeviceInfo>::value_type::Name, _1), (*pDevice)->Name) == 0) == m_cMonitoredDevices.end()) {
   }
}
Last edited on
main.cpp:26:72: error: no member named 'value_type' in 'boost::shared_ptr<DeviceInfo>'
      boost::bind(&strcmp, boost::bind(&boost::shared_ptr<DeviceInfo>::value_type::Name, _1), (*pDevice)->Name) == 0) == m_cMonitoredDevices.end()) {


could you post a minimal compilable example?

In any case, your code is almost fine, you're just comparing the result of find_if to the wrong container's end iterator.

The following changes made it work for me:

1
2
3
  if (std::find_if(cCurrentDevices.begin(), cCurrentDevices.end(),
                                     boost::bind(&strcmp, boost::bind(&DeviceInfo::Name, _1), (*pDevice)->Name) == 0 )
                 == cCurrentDevices.end())
Last edited on
Thanks you are right! I removed "shared_ptr<DeviceInfo>::value_type" and changed the contained iterator and it is working as expected now.

Thanks a lot!
Topic archived. No new replies allowed.