bind_noexcept_mf_test.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // bind_noexcept_mf_test.cpp
  3. //
  4. // Copyright 2017 Peter Dimov
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #include <boost/bind.hpp>
  11. #include <boost/ref.hpp>
  12. #include <boost/core/lightweight_test.hpp>
  13. #include <boost/config.hpp>
  14. #if defined(BOOST_NO_CXX11_NOEXCEPT)
  15. int main()
  16. {
  17. }
  18. #else
  19. //
  20. struct X
  21. {
  22. mutable unsigned int hash;
  23. X(): hash(0) {}
  24. int f0() noexcept { f1(17); return 0; }
  25. int g0() const noexcept { g1(17); return 0; }
  26. int f1(int a1) noexcept { hash = (hash * 17041 + a1) % 32768; return 0; }
  27. int g1(int a1) const noexcept { hash = (hash * 17041 + a1 * 2) % 32768; return 0; }
  28. int f2(int a1, int a2) noexcept { f1(a1); f1(a2); return 0; }
  29. int g2(int a1, int a2) const noexcept { g1(a1); g1(a2); return 0; }
  30. int f3(int a1, int a2, int a3) noexcept { f2(a1, a2); f1(a3); return 0; }
  31. int g3(int a1, int a2, int a3) const noexcept { g2(a1, a2); g1(a3); return 0; }
  32. int f4(int a1, int a2, int a3, int a4) noexcept { f3(a1, a2, a3); f1(a4); return 0; }
  33. int g4(int a1, int a2, int a3, int a4) const noexcept { g3(a1, a2, a3); g1(a4); return 0; }
  34. int f5(int a1, int a2, int a3, int a4, int a5) noexcept { f4(a1, a2, a3, a4); f1(a5); return 0; }
  35. int g5(int a1, int a2, int a3, int a4, int a5) const noexcept { g4(a1, a2, a3, a4); g1(a5); return 0; }
  36. int f6(int a1, int a2, int a3, int a4, int a5, int a6) noexcept { f5(a1, a2, a3, a4, a5); f1(a6); return 0; }
  37. int g6(int a1, int a2, int a3, int a4, int a5, int a6) const noexcept { g5(a1, a2, a3, a4, a5); g1(a6); return 0; }
  38. int f7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) noexcept { f6(a1, a2, a3, a4, a5, a6); f1(a7); return 0; }
  39. int g7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) const noexcept { g6(a1, a2, a3, a4, a5, a6); g1(a7); return 0; }
  40. int f8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) noexcept { f7(a1, a2, a3, a4, a5, a6, a7); f1(a8); return 0; }
  41. int g8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) const noexcept { g7(a1, a2, a3, a4, a5, a6, a7); g1(a8); return 0; }
  42. };
  43. void member_function_test()
  44. {
  45. X x;
  46. // 0
  47. boost::bind(&X::f0, &x)();
  48. boost::bind(&X::f0, boost::ref(x))();
  49. boost::bind(&X::g0, &x)();
  50. boost::bind(&X::g0, x)();
  51. boost::bind(&X::g0, boost::ref(x))();
  52. // 1
  53. boost::bind(&X::f1, &x, 1)();
  54. boost::bind(&X::f1, boost::ref(x), 1)();
  55. boost::bind(&X::g1, &x, 1)();
  56. boost::bind(&X::g1, x, 1)();
  57. boost::bind(&X::g1, boost::ref(x), 1)();
  58. // 2
  59. boost::bind(&X::f2, &x, 1, 2)();
  60. boost::bind(&X::f2, boost::ref(x), 1, 2)();
  61. boost::bind(&X::g2, &x, 1, 2)();
  62. boost::bind(&X::g2, x, 1, 2)();
  63. boost::bind(&X::g2, boost::ref(x), 1, 2)();
  64. // 3
  65. boost::bind(&X::f3, &x, 1, 2, 3)();
  66. boost::bind(&X::f3, boost::ref(x), 1, 2, 3)();
  67. boost::bind(&X::g3, &x, 1, 2, 3)();
  68. boost::bind(&X::g3, x, 1, 2, 3)();
  69. boost::bind(&X::g3, boost::ref(x), 1, 2, 3)();
  70. // 4
  71. boost::bind(&X::f4, &x, 1, 2, 3, 4)();
  72. boost::bind(&X::f4, boost::ref(x), 1, 2, 3, 4)();
  73. boost::bind(&X::g4, &x, 1, 2, 3, 4)();
  74. boost::bind(&X::g4, x, 1, 2, 3, 4)();
  75. boost::bind(&X::g4, boost::ref(x), 1, 2, 3, 4)();
  76. // 5
  77. boost::bind(&X::f5, &x, 1, 2, 3, 4, 5)();
  78. boost::bind(&X::f5, boost::ref(x), 1, 2, 3, 4, 5)();
  79. boost::bind(&X::g5, &x, 1, 2, 3, 4, 5)();
  80. boost::bind(&X::g5, x, 1, 2, 3, 4, 5)();
  81. boost::bind(&X::g5, boost::ref(x), 1, 2, 3, 4, 5)();
  82. // 6
  83. boost::bind(&X::f6, &x, 1, 2, 3, 4, 5, 6)();
  84. boost::bind(&X::f6, boost::ref(x), 1, 2, 3, 4, 5, 6)();
  85. boost::bind(&X::g6, &x, 1, 2, 3, 4, 5, 6)();
  86. boost::bind(&X::g6, x, 1, 2, 3, 4, 5, 6)();
  87. boost::bind(&X::g6, boost::ref(x), 1, 2, 3, 4, 5, 6)();
  88. // 7
  89. boost::bind(&X::f7, &x, 1, 2, 3, 4, 5, 6, 7)();
  90. boost::bind(&X::f7, boost::ref(x), 1, 2, 3, 4, 5, 6, 7)();
  91. boost::bind(&X::g7, &x, 1, 2, 3, 4, 5, 6, 7)();
  92. boost::bind(&X::g7, x, 1, 2, 3, 4, 5, 6, 7)();
  93. boost::bind(&X::g7, boost::ref(x), 1, 2, 3, 4, 5, 6, 7)();
  94. // 8
  95. boost::bind(&X::f8, &x, 1, 2, 3, 4, 5, 6, 7, 8)();
  96. boost::bind(&X::f8, boost::ref(x), 1, 2, 3, 4, 5, 6, 7, 8)();
  97. boost::bind(&X::g8, &x, 1, 2, 3, 4, 5, 6, 7, 8)();
  98. boost::bind(&X::g8, x, 1, 2, 3, 4, 5, 6, 7, 8)();
  99. boost::bind(&X::g8, boost::ref(x), 1, 2, 3, 4, 5, 6, 7, 8)();
  100. BOOST_TEST( x.hash == 23558 );
  101. }
  102. int main()
  103. {
  104. member_function_test();
  105. return boost::report_errors();
  106. }
  107. #endif