is_invocable.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright 2016-2017 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/poly_collection for library home page.
  7. */
  8. #ifndef BOOST_POLY_COLLECTION_DETAIL_IS_INVOCABLE_HPP
  9. #define BOOST_POLY_COLLECTION_DETAIL_IS_INVOCABLE_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <functional>
  14. #include <type_traits>
  15. /* technique explained at
  16. * http://bannalia.blogspot.com/2016/09/compile-time-checking-existence-of.html
  17. */
  18. namespace boost{
  19. namespace poly_collection{
  20. namespace detail{
  21. namespace is_invocable_fallback{
  22. template <typename F,typename... Args>
  23. struct is_invocable:
  24. std::is_constructible<
  25. std::function<void(Args...)>,
  26. std::reference_wrapper<typename std::remove_reference<F>::type>
  27. >
  28. {};
  29. template <typename R,typename F,typename... Args>
  30. struct is_invocable_r:
  31. std::is_constructible<
  32. std::function<R(Args...)>,
  33. std::reference_wrapper<typename std::remove_reference<F>::type>
  34. >
  35. {};
  36. struct hook{};
  37. }}}}
  38. namespace std{
  39. template<>
  40. struct is_void< ::boost::poly_collection::detail::is_invocable_fallback::hook>:
  41. std::false_type
  42. {
  43. template<typename F,typename... Args>
  44. static constexpr bool is_invocable_f()
  45. {
  46. using namespace ::boost::poly_collection::detail::is_invocable_fallback;
  47. return is_invocable<F,Args...>::value;
  48. }
  49. template<typename R,typename F,typename... Args>
  50. static constexpr bool is_invocable_r_f()
  51. {
  52. using namespace ::boost::poly_collection::detail::is_invocable_fallback;
  53. return is_invocable_r<R,F,Args...>::value;
  54. }
  55. };
  56. } /* namespace std */
  57. namespace boost{
  58. namespace poly_collection{
  59. namespace detail{
  60. template<typename F,typename... Args>
  61. struct is_invocable:std::integral_constant<
  62. bool,
  63. std::is_void<is_invocable_fallback::hook>::template
  64. is_invocable_f<F,Args...>()
  65. >{};
  66. template<typename R,typename F,typename... Args>
  67. struct is_invocable_r:std::integral_constant<
  68. bool,
  69. std::is_void<is_invocable_fallback::hook>::template
  70. is_invocable_r_f<R,F,Args...>()
  71. >{};
  72. } /* namespace poly_collection::detail */
  73. } /* namespace poly_collection */
  74. } /* namespace boost */
  75. #endif