1/*=============================================================================
2 Copyright (c) 2015 Paul Fultz II
3 can_be_called.h
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6==============================================================================*/
7
8#ifndef BOOST_HOF_GUARD_CAN_BE_CALLED_H
9#define BOOST_HOF_GUARD_CAN_BE_CALLED_H
10
11#include <boost/hof/config.hpp>
12#include <boost/hof/detail/and.hpp>
13#include <boost/hof/detail/holder.hpp>
14#include <boost/hof/detail/using.hpp>
15
16namespace boost { namespace hof { namespace detail {
17
18#if BOOST_HOF_NO_EXPRESSION_SFINAE
19struct dont_care
20{
21 dont_care(...);
22};
23
24template<class T>
25struct never_care
26{
27 typedef dont_care type;
28};
29
30struct cant_be_called_type
31{};
32
33struct no_type
34{};
35
36template<class F>
37struct is_callable_wrapper_fallback
38{
39 template<class... Ts>
40 auto operator()(Ts&&...) const
41 -> decltype(std::declval<F>()(std::declval<Ts>()...));
42};
43
44template<class T, class U=typename std::remove_cv<typename std::remove_reference<T>::type>::type>
45struct is_callable_wrapper_base
46: std::conditional<BOOST_HOF_IS_CLASS(U) && !BOOST_HOF_IS_FINAL(U), U, is_callable_wrapper_fallback<U>>
47{};
48
49template<class F, class... Ts>
50struct is_callable_wrapper : is_callable_wrapper_base<F>::type
51{
52 is_callable_wrapper();
53 typedef cant_be_called_type const &(*pointer_to_function)(typename never_care<Ts>::type...);
54 operator pointer_to_function() const;
55};
56
57template<class T>
58struct not_
59: std::integral_constant<bool, !T::value>
60{};
61
62template<class F, class... Ts>
63struct can_be_called
64: not_<std::is_same<cant_be_called_type, typename std::decay<decltype(
65 is_callable_wrapper<F, Ts...>()(std::declval<Ts>()...)
66)>::type>>
67{};
68
69template<class F, class... Ts>
70struct check_args;
71
72template<class Res, class... Ts, class... Us>
73struct check_args<Res(Us...), Ts...>
74: and_<std::is_convertible<Ts, Us>...>
75{};
76
77template<class Res, class... Ts, class... Us>
78struct can_be_called<Res(*)(Us...), Ts...>
79: std::conditional<sizeof...(Ts) == sizeof...(Us),
80 check_args<Res(Us...), Ts...>,
81 std::false_type
82>::type
83{};
84
85template<class Res, class... Ts, class... Us>
86struct can_be_called<Res(Us...), Ts...>
87: std::conditional<sizeof...(Ts) == sizeof...(Us),
88 check_args<Res(Us...), Ts...>,
89 std::false_type
90>::type
91{};
92
93#else
94
95template<class T>
96T&& called_val() noexcept;
97
98template<class... Ts>
99struct callable_args
100{};
101
102template<class F, class Args, class=void>
103struct can_be_called_impl
104: std::false_type
105{};
106
107template<class F, class... Args>
108struct can_be_called_impl<F, callable_args<Args...>, typename detail::holder<
109 decltype( boost::hof::detail::called_val<F>()(boost::hof::detail::called_val<Args>()...) )
110>::type>
111: std::true_type
112{};
113
114template<class F, class... Ts>
115BOOST_HOF_USING(can_be_called, can_be_called_impl<F, detail::callable_args<Ts...>>);
116
117#endif
118
119}}} // namespace boost::hof
120
121#endif
122

source code of boost/libs/hof/include/boost/hof/detail/can_be_called.hpp