Search code examples
c++c++11boostboost-graph

using boost bind for function object with a state and two arguments in operator()()


I am having some trouble in doing the following : using boost::bind / std::bind in a boost graph.

//function object 
template  <typename graph_t>
struct my_functor {
 public: 
   my_functor() {  } 
   my_functor(graph_t&  _G) : G(_G) {   } 

  template <typename edge_t>
  bool operator() (const edge_t&   edge1, const edge_1&  edge2) {
     //do something with edge properties. 
     // return true or false as per condition
  }
 private:
   graph_t&  G;
 };

 //boost graph 
 Graph_t   G;  // add some edges /properties etc. 

 //store the edges in some container
 edge_container_t   edge_set;   //edge_container_t  is say a std::vector<edge_descriptor>

 //now the code
 my_functor<graph_t>  func(G);  //initialize a function 

 //I want to bind by function object here so that it accepts two edges  in the operator()() of function 
 //object. 

 auto bind_func = std::bind(func, _1, _2);  //_1, _2  should take edge1 and edge2 as arguments


 for (const auto& edge1 : edge_set) {
    for (const auto& edge2 : edge_set { 
       if (edge1 != edge2) 
        bool y = bind_func(edge1, edge2)  // bind_func  returns a bool 
    }
  }

I am doing something wrong here while creating bind_func, but I am a bit confused with the documentation. How would I do this if I want to use boost::bind ??

Please help in this case. Some errors I get using trial and error: 1. placeholder _1 , _2 was not declared in scope ( I did include still it comes 2. if I use boost::bind in place of std::bind, it gives some unwrapper error.


Solution

  • The placeholders are in the namespace std::placeholders:

    auto bind_func = std::bind(f, std::placeholders::_1, 
                                  std::placeholders::_2);  //_1, _2  should take edge1 and edge2 as arguments
    

    Should compile fine. I don't really see though why you bind f to nothing, using bind_func has no advantage compared to f.