2
0
mirror of https://github.com/boostorg/compat.git synced 2026-01-19 04:02:16 +00:00

Add bind_front

This commit is contained in:
Peter Dimov
2024-03-27 21:41:06 +02:00
parent c290c03ea3
commit 90e76cb4f4
4 changed files with 125 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
#ifndef BOOST_COMPAT_BIND_FRONT_HPP_INCLUDED
#define BOOST_COMPAT_BIND_FRONT_HPP_INCLUDED
// 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/compat/type_traits.hpp>
#include <boost/compat/detail/returns.hpp>
#include <boost/mp11/integer_sequence.hpp>
#include <boost/config.hpp>
#include <boost/config/workaround.hpp>
#include <tuple>
#include <utility>
namespace boost {
namespace compat {
namespace detail {
#if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
# pragma warning(push)
# pragma warning(disable: 4100) // 'a': unreferenced formal parameter
#endif
template<class F, class A, class... B, std::size_t... I>
static constexpr auto invoke_bind_front_( F&& f, A&& a, mp11::index_sequence<I...>, B&&... b )
BOOST_COMPAT_RETURNS( compat::invoke( std::forward<F>(f), std::get<I>( std::forward<A>(a) )..., std::forward<B>(b)... ) )
#if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
# pragma warning(pop)
#endif
template<class F, class... A> class bind_front_
{
private:
F f_;
std::tuple<A...> a_;
public:
template<class F2, class... A2>
constexpr bind_front_( F2&& f2, A2&&... a2 ): f_( std::forward<F2>(f2) ), a_( std::forward<A2>(a2)... ) {}
public:
template<class... B> BOOST_CXX14_CONSTEXPR auto operator()( B&&... b ) &
BOOST_COMPAT_RETURNS( detail::invoke_bind_front_( f_, a_, mp11::make_index_sequence<sizeof...(A)>(), std::forward<B>(b)... ) )
template<class... B> constexpr auto operator()( B&&... b ) const &
BOOST_COMPAT_RETURNS( detail::invoke_bind_front_( f_, a_, mp11::make_index_sequence<sizeof...(A)>(), std::forward<B>(b)... ) )
template<class... B> BOOST_CXX14_CONSTEXPR auto operator()( B&&... b ) &&
BOOST_COMPAT_RETURNS( detail::invoke_bind_front_( std::move(f_), std::move(a_), mp11::make_index_sequence<sizeof...(A)>(), std::forward<B>(b)... ) )
template<class... B> constexpr auto operator()( B&&... b ) const &&
BOOST_COMPAT_RETURNS( detail::invoke_bind_front_( std::move(f_), std::move(a_), mp11::make_index_sequence<sizeof...(A)>(), std::forward<B>(b)... ) )
};
} // namespace detail
template<class F, class... A> constexpr auto bind_front( F&& f, A&&... a ) -> detail::bind_front_< decay_t<F>, decay_t<A>... >
{
return { std::forward<F>(f), std::forward<A>(a)... };
}
} // namespace compat
} // namespace boost
#endif // BOOST_COMPAT_BIND_FRONT_HPP_INCLUDED

View File

@@ -15,6 +15,8 @@ template<class T> using remove_cv_t = typename std::remove_cv<T>::type;
template<class T> using remove_reference_t = typename std::remove_reference<T>::type;
template<class T> using remove_cvref_t = remove_cv_t< remove_reference_t<T> >;
template<class T> using decay_t = typename std::decay<T>::type;
template<bool B, class T = void> using enable_if_t = typename std::enable_if<B, T>::type;
template<bool B, class T, class F> using conditional_t = typename std::conditional<B, T, F>::type;

View File

@@ -52,3 +52,5 @@ compile invoke_md_constexpr_test.cpp
run invoke_result_test.cpp ;
run is_invocable_test.cpp ;
run is_nothrow_invocable_test.cpp ;
run bind_front_fn_test.cpp ;

View File

@@ -0,0 +1,49 @@
// Copyright 2024 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#if defined(__GNUC__) && __GNUC__ == 7
# pragma GCC diagnostic ignored "-Wnoexcept-type"
#endif
#include <boost/compat/bind_front.hpp>
#include <boost/core/lightweight_test.hpp>
int f0()
{
return -1;
}
int f1( int x1 ) noexcept
{
return x1;
}
int f2( int x1, int x2 )
{
return 10*x1+x2;
}
int f3( int x1, int x2, int x3 ) noexcept
{
return 100*x1 + 10*x2 + x3;
}
int main()
{
BOOST_TEST_EQ( boost::compat::bind_front( f0 )(), -1 );
BOOST_TEST_EQ( boost::compat::bind_front( f1 )( 1 ), 1 );
BOOST_TEST_EQ( boost::compat::bind_front( f2 )( 1, 2 ), 12 );
BOOST_TEST_EQ( boost::compat::bind_front( f3 )( 1, 2, 3 ), 123 );
BOOST_TEST_EQ( boost::compat::bind_front( f1, 1 )(), 1 );
BOOST_TEST_EQ( boost::compat::bind_front( f2, 1 )( 2 ), 12 );
BOOST_TEST_EQ( boost::compat::bind_front( f3, 1 )( 2, 3 ), 123 );
BOOST_TEST_EQ( boost::compat::bind_front( f2, 1, 2 )(), 12 );
BOOST_TEST_EQ( boost::compat::bind_front( f3, 1, 2 )( 3 ), 123 );
BOOST_TEST_EQ( boost::compat::bind_front( f3, 1, 2, 3 )(), 123 );
return boost::report_errors();
}