Skip to content

Commit

Permalink
Add is_nothrow_invocable
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Mar 22, 2024
1 parent 26e18fd commit 5b6c4ff
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
11 changes: 11 additions & 0 deletions include/boost/compat/invoke.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ template<class F, class... A> struct is_invocable_< void_t<invoke_result_t<F, A.

template<class F, class... A> struct is_invocable: detail::is_invocable_<void, F, A...> {};

// is_nothrow_invocable

namespace detail {

template<class, class F, class... A> struct is_nothrow_invocable_: std::false_type {};
template<class F, class... A> struct is_nothrow_invocable_< enable_if_t< noexcept( compat::invoke( std::declval<F>(), std::declval<A>()... ) ) >, F, A... >: std::true_type {};

} // namespace detail

template<class F, class... A> struct is_nothrow_invocable: detail::is_nothrow_invocable_<void, F, A...> {};

} // 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 @@ -51,3 +51,4 @@ compile invoke_md_constexpr_test.cpp

run invoke_result_test.cpp ;
run is_invocable_test.cpp ;
run is_nothrow_invocable_test.cpp ;
132 changes: 132 additions & 0 deletions test/is_nothrow_invocable_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// 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_nothrow_invocable;

// nonfunction

BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<int> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<int, int> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<int, int, int> ));

// function reference

BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<void(&)()> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<char(&)(int), char> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<int(&)(int, int), int, int> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<double(&)(double, double, double), float, float, float> ));

#if defined(__cpp_noexcept_function_type)

BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<void(&)() noexcept> ));
BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<char(&)(int) noexcept, char> ));
BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<int(&)(int, int) noexcept, int, int> ));
BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<double(&)(double, double, double) noexcept, float, float, float> ));

#endif

BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<void(&)(), int> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<char(&)(int)> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<char(&)(int), int, int> ));

// function pointer

BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<void(*)()> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<char(*)(int), char> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<int(*)(int, int), int, int> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<double(*)(double, double, double), float, float, float> ));

#if defined(__cpp_noexcept_function_type)

BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<void(*)() noexcept> ));
BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<char(*)(int) noexcept, char> ));
BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<int(*)(int, int) noexcept, int, int> ));
BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<double(*)(double, double, double) noexcept, float, float, float> ));

#endif

BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<void(*)(), int> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<char(*)(int)> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<char(*)(int), int, int> ));

// object

BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<F> ));
BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<F, char> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<F, int, int> ));
BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<F, float, float, float> ));

BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<F, int, int, int, int> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<F const> ));

// member function pointer

BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<void(X::*)(), X> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<char(X::*)(int), X, char> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<int(X::*)(int, int), X, int, int> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<double(X::*)(double, double, double), X, float, float, float> ));

#if defined(__cpp_noexcept_function_type)

BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<void(X::*)() noexcept, X> ));
BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<char(X::*)(int) noexcept, X, char> ));
BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<int(X::*)(int, int) noexcept, X, int, int> ));
BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<double(X::*)(double, double, double) noexcept, X, float, float, float> ));

#endif

BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<void(X::*)()> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<void(X::*)(), int> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<void(X::*)(), X, int> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<char(X::*)(int)> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<char(X::*)(int), int> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<char(X::*)(int), X> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<char(X::*)(int), X, int, int> ));

// member data pointer

BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<int X::*, X> ));
BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<int X::*, X const> ));
BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<int X::*, X&> ));
BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<int X::*, X const&> ));
BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<int X::*, X*> ));
BOOST_TEST_TRAIT_TRUE(( is_nothrow_invocable<int X::*, X const*> ));

BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<int X::*> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<int X::*, int> ));
BOOST_TEST_TRAIT_FALSE(( is_nothrow_invocable<int X::*, X, int> ));

return boost::report_errors();
}

0 comments on commit 5b6c4ff

Please sign in to comment.