【问题标题】:using boost bind for function object with a state and two arguments in operator()()在 operator()() 中对具有状态和两个参数的函数对象使用 boost 绑定
【发布时间】:2014-07-21 05:21:13
【问题描述】:

我在执行以下操作时遇到了一些麻烦:在 boost 图中使用 boost::bind / std::bind。

//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 
    }
  }

我在创建 bind_func 时在这里做错了,但我对文档有点困惑。 如果我想使用 boost::bind 怎么办?

请在这种情况下提供帮助。 我使用反复试验得到的一些错误: 1.占位符 _1 , _2 未在范围内声明(我确实包括它仍然存在 2. 如果我使用 boost::bind 代替 std::bind,它会给出一些解包错误。

【问题讨论】:

    标签: c++ c++11 boost boost-graph


    【解决方案1】:

    占位符在命名空间std::placeholders

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

    应该可以正常编译。 我真的不明白为什么你将f 绑定到任何东西,使用bind_funcf 相比没有优势。

    【讨论】:

    • 谢谢。这是一个愚蠢的错误。 boost::bind 也有效。我将 作为模板参数传递给绑定,这给出了错误。另外是否可以将 bind_func 作为模板参数传递?仿函数在其他文件中定义,我想绑定图形并将 bind_func 作为模板参数传递给其他仿函数?
    猜你喜欢
    • 1970-01-01
    • 2010-10-02
    • 2011-05-01
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多