-1

プレースホルダーを追加するときに少し問題がstd::bind発生しました 私のコードはちょっと大きいので、要点だけにとどめます

#define GETFUNC(a) (std::bind(&app::a, this, std::placeholders::_1))
class button{
button(<other parameters here>, std::function<void(int)>) { ... }
..
std::function<void(int)> onhover;
..
};

class app{
app(){
elements.push_back(buttonPtr( new button(<other parameters>, GETFUNC(onHover) );
..
typedef std::unique_ptr<button> buttonPtr;
std::vector<buttonPtr> elements;
..
void onHover(int i) {}
}

そのコードのビットは(エラーログstd::bindから取得したほど)失敗しますが、変更すると機能します:

  • すべてstd::function<void(int)>std::function<void()>
  • onHover(int i)onHover()
  • std::bind(&app::a, this, std::placeholders::_1)std::bind(&app::a, this)

なぜこれが起こるのか、どうすれば修正できるのかについて何か考えはありますか?

4

1 に答える 1

1

それはうまくいっています。これを確認して、コードとの違いを探してください。

#include <functional>
#include <iostream>


struct app
{
  std::function<void (int)>
  get_func ()
  {
    return std::bind (&app::on_hover, this, std::placeholders::_1);
  }

  void on_hover (int v)
  {
    std::cout << "it works: " << v << std::endl;
  }
};

int
main ()
{
  app a;

  auto f = a.get_func ();
  f (5);
}
于 2012-11-27T21:59:02.603 に答える