depr.adaptors.cxx1z.pass.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <functional>
  9. // In C++17, the function adapters mem_fun/mem_fun_ref, etc have been removed.
  10. // However, for backwards compatibility, if _LIBCPP_ENABLE_CXX17_REMOVED_BINDERS
  11. // is defined before including <functional>, then they will be restored.
  12. // MODULES_DEFINES: _LIBCPP_ENABLE_CXX17_REMOVED_BINDERS
  13. #define _LIBCPP_ENABLE_CXX17_REMOVED_BINDERS
  14. #include <functional>
  15. #include <cassert>
  16. int identity(int v) { return v; }
  17. int sum(int a, int b) { return a + b; }
  18. struct Foo {
  19. int zero() { return 0; }
  20. int zero_const() const { return 1; }
  21. int identity(int v) const { return v; }
  22. int sum(int a, int b) const { return a + b; }
  23. };
  24. int main(int, char**)
  25. {
  26. typedef std::pointer_to_unary_function<int, int> PUF;
  27. typedef std::pointer_to_binary_function<int, int, int> PBF;
  28. static_assert(
  29. (std::is_same<PUF, decltype((std::ptr_fun<int, int>(identity)))>::value),
  30. "");
  31. static_assert(
  32. (std::is_same<PBF, decltype((std::ptr_fun<int, int, int>(sum)))>::value),
  33. "");
  34. assert((std::ptr_fun<int, int>(identity)(4) == 4));
  35. assert((std::ptr_fun<int, int, int>(sum)(4, 5) == 9));
  36. Foo f;
  37. assert((std::mem_fn(&Foo::identity)(f, 5) == 5));
  38. assert((std::mem_fn(&Foo::sum)(f, 5, 6) == 11));
  39. typedef std::mem_fun_ref_t<int, Foo> MFR;
  40. typedef std::const_mem_fun_ref_t<int, Foo> CMFR;
  41. static_assert(
  42. (std::is_same<MFR, decltype((std::mem_fun_ref(&Foo::zero)))>::value), "");
  43. static_assert((std::is_same<CMFR, decltype((std::mem_fun_ref(
  44. &Foo::zero_const)))>::value),
  45. "");
  46. assert((std::mem_fun_ref(&Foo::zero)(f) == 0));
  47. assert((std::mem_fun_ref(&Foo::identity)(f, 5) == 5));
  48. return 0;
  49. }