简体   繁体   English

使用find_if和boost :: bind与一组shared_pointers

[英]Using find_if and boost::bind with a set of shared_pointers

I have a vector of shared_ptr, I want to combine boost shared_ptr and bind together. 我有一个shared_ptr的向量,我想将boost shared_ptr结合起来并绑定在一起。

My question is very similar to this , except that instead of "&MyClass::ReferenceFn" I would like to call "&Element::Fn". 我的问题与非常相似,除了我想用“&Element :: Fn”代替“&MyClass :: ReferenceFn”。

Here is a similar piece of code: 这是一段类似的代码:

typedef boost::shared_ptr< Vertex > vertex_ptr; 
std::set<vertex_ptr> vertices;

void B::convert()
{
...
if( std::find_if(boost::make_indirect_iterator(vertices.begin()), 
                 boost::make_indirect_iterator(vertices.end() ),  boost::bind( &Vertex::id, boost::ref(*this), _1 ) == (*it)->id() ) == vertices.end() )
}

here is the error: 这是错误:

no matching function for call to ‘bind(<unresolved overloaded function type>, const boost::reference_wrapper<B>, boost::arg<1>&)’

NOTE : I am limited to use the C++03. 注意 :我仅限使用C ++ 03。

To call a member function for each object stored in a collection, you need to use a placeholder as the first bound argument of the boost::bind : 要为集合中存储的每个对象调用成员函数,您需要使用占位符作为boost::bind的第一个绑定参数:

boost::bind(&Vertex::id, _1) == (*it)->id())
//                       ~^~

This way, each argument a , will be bound to a member function pointer, and called as (a.*&Vertex::id)() . 这样,每个参数a都将绑定到成员函数指针,并称为(a.*&Vertex::id)()

However, seeing that the error message says unresolved overloaded function type , it draws a conclusion that your class Vertex can have multiple overloads of member function id . 但是,看到错误消息显示了unresolved overloaded function type ,因此得出结论,您的类Vertex可以具有成员函数id多个重载。 As such, the compiler is unable to tell which one it should pass as the argument of boost::bind . 因此,编译器无法将其传递给boost::bind的参数。 To solve this, use an explicit cast to a member function pointer (the asterisk after colon indicates it's a pointer to a member): 要解决此问题,请使用显式强制转换为成员函数指针(冒号后的星号表示它是指向成员的指针):

boost::bind(static_cast<int(Vertex::*)()const>(&Vertex::id), _1) == (*it)->id())
//                      ~~~~~~~~~~~~~~~~~~~~^

In the case class Vertex has multiple overloads, say: 在类Vertex具有多个重载的情况下,请说:

int id() const { return 0; }
void id(int i) { }

you will use the first one for binding. 您将使用第一个进行绑定。

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

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