LoginSignup
0
1

More than 5 years have passed since last update.

std::function operator ==で関数同士を比較できない

Posted at

プログラムを書いていて、ハマることあるよね。
ハマらなければ、スーパープログラマーなんだけどね
今回ハマったのが、std::function
まあ関数ポインタみたいなもんでしょ
って軽い感じで考えていました。
サクッと実装しようとしたら、STLの長ったらしいコンパイルエラー・・・

#include <list>
#include <iostream>
#include <functional>

int main()
{
  std::function<int(int)> f, g;
  std::list<std::function<int(int)>> li;
  li.push_back(f);
  li.push_back(g);
  li.remove(f);//こいつがNGなわけだ
}

なになに、「 no match for ‘operator==’ (operand types are ‘std::function・・・」
いやいや・・std::functionに==あるっしょと思ったけど・・

#include <list>
#include <iostream>
#include <functional>

int ident(int x) { return x; }

int main()
{
  std::function<int(int)> f, g;
  g = ident;
  if (f == g) {//やっぱりNG
   std::cout << "ok" << std::endl;
  }
}

はい、だめです。できません。
stackoverflowの意見
公式が無理って言ってるから無理なんでしょう

それでも無理やりポインタ使って比較する

ハマった挙句、ダメだったのでしばらく散歩します。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1