mirror of
https://github.com/boostorg/compat.git
synced 2026-01-19 04:02:16 +00:00
Add is_invocable
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
// https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#include <boost/compat/mem_fn.hpp>
|
||||
#include <boost/compat/type_traits.hpp>
|
||||
#include <boost/compat/detail/returns.hpp>
|
||||
#include <utility>
|
||||
|
||||
@@ -26,6 +27,17 @@ BOOST_COMPAT_RETURNS( mem_fn(pm)(std::forward<A>(a)...) )
|
||||
|
||||
template<class F, class... A> using invoke_result_t = decltype( compat::invoke( std::declval<F>(), std::declval<A>()... ) );
|
||||
|
||||
// is_invocable
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<class, class F, class... A> struct is_invocable_: std::false_type {};
|
||||
template<class F, class... A> struct is_invocable_< void_t<invoke_result_t<F, A...>>, F, A... >: std::true_type {};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<class F, class... A> struct is_invocable: detail::is_invocable_<void, F, A...> {};
|
||||
|
||||
} // namespace compat
|
||||
} // namespace boost
|
||||
|
||||
|
||||
@@ -17,6 +17,17 @@ template<class T> using remove_cvref_t = remove_cv_t< remove_reference_t<T> >;
|
||||
|
||||
template<bool B, class T = void> using enable_if_t = typename std::enable_if<B, T>::type;
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<class...> struct make_void
|
||||
{
|
||||
using type = void;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<class... T> using void_t = typename detail::make_void<T...>::type;
|
||||
|
||||
} // namespace compat
|
||||
} // namespace boost
|
||||
|
||||
|
||||
@@ -50,3 +50,4 @@ compile invoke_md_constexpr_test.cpp
|
||||
: <toolset>gcc-4.8:<build>no ;
|
||||
|
||||
run invoke_result_test.cpp ;
|
||||
run is_invocable_test.cpp ;
|
||||
|
||||
@@ -96,6 +96,8 @@ int main()
|
||||
BOOST_TEST_TRAIT_SAME( invoke_result_t<int X::*, X const>, int const&& );
|
||||
BOOST_TEST_TRAIT_SAME( invoke_result_t<int X::*, X&>, int& );
|
||||
BOOST_TEST_TRAIT_SAME( invoke_result_t<int X::*, X const&>, int const& );
|
||||
BOOST_TEST_TRAIT_SAME( invoke_result_t<int X::*, X*>, int& );
|
||||
BOOST_TEST_TRAIT_SAME( invoke_result_t<int X::*, X const*>, int const& );
|
||||
|
||||
BOOST_TEST_TRAIT_FALSE(( mp_valid<invoke_result_t, int X::*> ));
|
||||
BOOST_TEST_TRAIT_FALSE(( mp_valid<invoke_result_t, int X::*, int> ));
|
||||
|
||||
105
test/is_invocable_test.cpp
Normal file
105
test/is_invocable_test.cpp
Normal file
@@ -0,0 +1,105 @@
|
||||
// Copyright 2024 Peter Dimov
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#include <boost/compat/invoke.hpp>
|
||||
#include <boost/core/lightweight_test_trait.hpp>
|
||||
|
||||
struct F
|
||||
{
|
||||
void operator()()
|
||||
{
|
||||
}
|
||||
|
||||
char operator()( char x1 ) noexcept
|
||||
{
|
||||
return x1;
|
||||
}
|
||||
|
||||
int operator()( int x1, int x2 ) const
|
||||
{
|
||||
return 10*x1+x2;
|
||||
}
|
||||
|
||||
double operator()( float x1, float x2, float x3 ) const noexcept
|
||||
{
|
||||
return 100*x1 + 10*x2 + x3;
|
||||
}
|
||||
};
|
||||
|
||||
struct X
|
||||
{
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
using boost::compat::is_invocable;
|
||||
|
||||
// nonfunction
|
||||
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<int> ));
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<int, int> ));
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<int, int, int> ));
|
||||
|
||||
// function reference
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<void(&)()> ));
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<char(&)(int), char> ));
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<int(&)(int, int), int, int> ));
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<double(&)(double, double, double), float, float, float> ));
|
||||
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<void(&)(), int> ));
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<char(&)(int)> ));
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<char(&)(int), int, int> ));
|
||||
|
||||
// function pointer
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<void(*)()> ));
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<char(*)(int), char> ));
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<int(*)(int, int), int, int> ));
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<double(*)(double, double, double), float, float, float> ));
|
||||
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<void(*)(), int> ));
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<char(*)(int)> ));
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<char(*)(int), int, int> ));
|
||||
|
||||
// object
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<F> ));
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<F, char> ));
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<F, int, int> ));
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<F, float, float, float> ));
|
||||
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<F, int, int, int, int> ));
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<F const> ));
|
||||
|
||||
// member function pointer
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<void(X::*)(), X> ));
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<char(X::*)(int), X, char> ));
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<int(X::*)(int, int), X, int, int> ));
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<double(X::*)(double, double, double), X, float, float, float> ));
|
||||
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<void(X::*)()> ));
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<void(X::*)(), int> ));
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<void(X::*)(), X, int> ));
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<char(X::*)(int)> ));
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<char(X::*)(int), int> ));
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<char(X::*)(int), X> ));
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<char(X::*)(int), X, int, int> ));
|
||||
|
||||
// member data pointer
|
||||
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<int X::*, X> ));
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<int X::*, X const> ));
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<int X::*, X&> ));
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<int X::*, X const&> ));
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<int X::*, X*> ));
|
||||
BOOST_TEST_TRAIT_TRUE(( is_invocable<int X::*, X const*> ));
|
||||
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<int X::*> ));
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<int X::*, int> ));
|
||||
BOOST_TEST_TRAIT_FALSE(( is_invocable<int X::*, X, int> ));
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
Reference in New Issue
Block a user