Skip to content

Commit

Permalink
Add is_invocable
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Mar 22, 2024
1 parent 571b48c commit 26e18fd
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/boost/compat/invoke.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand All @@ -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

Expand Down
11 changes: 11 additions & 0 deletions include/boost/compat/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions test/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
2 changes: 2 additions & 0 deletions test/invoke_result_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> ));
Expand Down
105 changes: 105 additions & 0 deletions test/is_invocable_test.cpp
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 26e18fd

Please sign in to comment.