fork download
  1. #include <vector>
  2. #include <algorithm>
  3. #include <iterator>
  4. #include <iostream>
  5. namespace
  6. {
  7. class placeholder {};
  8. placeholder _1;
  9. }
  10.  
  11. class Test
  12. {
  13. public:
  14. Test() {}
  15. Test(const Test& p) {}
  16. ~Test() {}
  17. void do_stuff(const std::vector<int>& v)
  18. {
  19. std::vector<int>::const_iterator it = v.begin();
  20. for (; it != v.end(); ++it) {
  21. std::cout << *it << std::endl;
  22. }
  23. }
  24. };
  25.  
  26. class simple_bind_t
  27. {
  28. typedef void(Test::*fn)(const std::vector<int>&);
  29. fn fn_;
  30. Test t_;
  31. public:
  32. simple_bind_t(fn f, const Test& t): fn_(f), t_(t)
  33. {
  34. int i = 0;
  35. }
  36. ~simple_bind_t()
  37. {
  38. int i = 0;
  39. }
  40.  
  41. void operator()(const std::vector<int>& a)
  42. {
  43. return (t_.*fn_)(a);
  44. }
  45. };
  46.  
  47. simple_bind_t simple_bind(void(Test::*fn)(const std::vector<int>&),
  48. const Test& t, const placeholder&)
  49. {
  50. return simple_bind_t(fn, t);
  51. }
  52.  
  53. int main()
  54. {
  55. Test t;
  56. //boost::shared_ptr<Test> ptr_test = boost::make_shared<Test>();
  57. std::vector<int> vec;
  58. vec.push_back(42);
  59. simple_bind(&Test::do_stuff, t, _1)(vec); //等同于下面2行代码逻辑
  60. return 0;
  61. }
Success #stdin #stdout 0s 2872KB
stdin
Standard input is empty
stdout
42