apply_member_pointer.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*<-
  2. Copyright (c) 2016 Barrett Adair
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  5. ->*/
  6. //[ apply_member_pointer
  7. #include <type_traits>
  8. #include <boost/callable_traits/apply_member_pointer.hpp>
  9. namespace ct = boost::callable_traits;
  10. struct foo;
  11. struct bar;
  12. int main() {
  13. {
  14. // function type -> member function pointer type
  15. using f = int(int);
  16. using g = ct::apply_member_pointer_t<f, foo>;
  17. using expect = int(foo::*)(int);
  18. static_assert(std::is_same<g, expect>::value, "");
  19. }
  20. {
  21. // function pointer type (unqualified) -> member function pointer type
  22. using f = int(*)();
  23. using g = ct::apply_member_pointer_t<f, foo>;
  24. using expect = int(foo::*)();
  25. static_assert(std::is_same<g, expect>::value, "");
  26. }
  27. {
  28. // function pointer type (qualified) -> member data pointer type
  29. // Look out for cases like these two. If the input type to apply_member_pointer
  30. // is a ``[*qualified]`` function pointer type, then the aliased type is a member data
  31. // pointer to a ``[*qualified function pointer]``.
  32. {
  33. using f = int(*&)();
  34. using g = ct::apply_member_pointer_t<f, foo>;
  35. using expect = int (* foo::*)();
  36. static_assert(std::is_same<g, expect>::value, "");
  37. }
  38. {
  39. using f = int(* const)();
  40. using g = ct::apply_member_pointer_t<f, foo>;
  41. using expect = int (* const foo::*)();
  42. static_assert(std::is_same<g, expect>::value, "");
  43. }
  44. }
  45. {
  46. // function reference type -> member function pointer type
  47. using f = void(&)();
  48. using g = ct::apply_member_pointer_t<f, foo>;
  49. using expect = void(foo::*)();
  50. static_assert(std::is_same<g, expect>::value, "");
  51. }
  52. {
  53. // member function pointer type -> member function pointer type
  54. // (note the different parent class)
  55. using f = int(bar::*)() const;
  56. using g = ct::apply_member_pointer_t<f, foo>;
  57. using expect = int(foo::*)() const;
  58. static_assert(std::is_same<g, expect>::value, "");
  59. }
  60. {
  61. // non-callable type -> member data pointer type
  62. using g = ct::apply_member_pointer_t<int, foo>;
  63. using expect = int foo::*;
  64. static_assert(std::is_same<g, expect>::value, "");
  65. }
  66. {
  67. // function object type -> member data pointer type
  68. // the same is true for lambdas and generic lambdas
  69. auto lambda = [](){};
  70. using f = decltype(lambda);
  71. using g = ct::apply_member_pointer_t<f, foo>;
  72. using expect = f foo::*;
  73. static_assert(std::is_same<g, expect>::value, "");
  74. }
  75. }
  76. //]