fork(1) download
  1. #include <functional>
  2. #include <iostream>
  3.  
  4. struct foo {
  5. double operator()(int a, long b) { return a + b; }
  6.  
  7. /// 等値演算子☆彡
  8. bool operator==(const foo& rhs) {
  9. std::cout << __FUNCTION__ << ": this=" << this << ", &rhs=" << &rhs << std::endl;
  10. return (this == &rhs);
  11. }
  12. };
  13.  
  14. int main(void) {
  15. foo quuux;
  16. foo quuuux;
  17. std::function<double(int, long)> gfred = quuux;
  18.  
  19. std::cout << "value=" << (*gfred.target<foo>())(10, 200L) << std::endl; // "value=210" と表示される(意図通りイゴイタ
  20.  
  21. if (&*gfred.target<foo>() == &quuux) {
  22. std::cout << "gfred is quuux" << std::endl; // 表示されない
  23. }
  24. if (*gfred.target<foo>() == quuux) {
  25. std::cout << "gfred equals quuux" << std::endl; // "gfred equals quuux" と表示されて欲しいがされない
  26. }
  27. if (*gfred.target<foo>() == quuuux) {
  28. std::cout << "gfred equals quuuux" << std::endl; // 表示されない
  29. }
  30.  
  31. // 等値演算子☆彡のテスト
  32. // 以下の結果を見る限り、等値演算子☆彡自体は正しく動いている。
  33. if (quuux == quuux) {
  34. std::cout << "quuux == quuux" << std::endl; // "quuux == quuux" と表示される
  35. }
  36. if (quuux == quuuux) {
  37. std::cout << "quuux == quuuux" << std::endl; // 表示されない
  38. }
  39. }
  40.  
Success #stdin #stdout 0s 5328KB
stdin
Standard input is empty
stdout
value=210
operator==: this=0x7ffe83bbcb90, &rhs=0x7ffe83bbcb8e
operator==: this=0x7ffe83bbcb90, &rhs=0x7ffe83bbcb8f
operator==: this=0x7ffe83bbcb8e, &rhs=0x7ffe83bbcb8e
quuux == quuux
operator==: this=0x7ffe83bbcb8e, &rhs=0x7ffe83bbcb8f