bind_front.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #ifndef BOOST_COMPAT_BIND_FRONT_HPP_INCLUDED
  2. #define BOOST_COMPAT_BIND_FRONT_HPP_INCLUDED
  3. // Copyright 2024 Peter Dimov
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt
  6. #include <boost/compat/invoke.hpp>
  7. #include <boost/compat/type_traits.hpp>
  8. #include <boost/compat/integer_sequence.hpp>
  9. #include <boost/compat/detail/returns.hpp>
  10. #include <boost/config.hpp>
  11. #include <boost/config/workaround.hpp>
  12. #include <tuple>
  13. #include <utility>
  14. namespace boost {
  15. namespace compat {
  16. namespace detail {
  17. #if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
  18. # pragma warning(push)
  19. # pragma warning(disable: 4100) // 'a': unreferenced formal parameter
  20. #endif
  21. template<class F, class A, class... B, std::size_t... I>
  22. static constexpr auto invoke_bind_front_( F&& f, A&& a, index_sequence<I...>, B&&... b )
  23. BOOST_COMPAT_RETURNS( compat::invoke( std::forward<F>(f), std::get<I>( std::forward<A>(a) )..., std::forward<B>(b)... ) )
  24. #if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
  25. # pragma warning(pop)
  26. #endif
  27. template<class F, class... A> class bind_front_
  28. {
  29. private:
  30. F f_;
  31. std::tuple<A...> a_;
  32. public:
  33. template<class F2, class... A2>
  34. constexpr bind_front_( F2&& f2, A2&&... a2 ): f_( std::forward<F2>(f2) ), a_( std::forward<A2>(a2)... ) {}
  35. public:
  36. template<class... B> BOOST_CXX14_CONSTEXPR auto operator()( B&&... b ) &
  37. BOOST_COMPAT_RETURNS( detail::invoke_bind_front_( f_, a_, make_index_sequence<sizeof...(A)>(), std::forward<B>(b)... ) )
  38. template<class... B> constexpr auto operator()( B&&... b ) const &
  39. BOOST_COMPAT_RETURNS( detail::invoke_bind_front_( f_, a_, make_index_sequence<sizeof...(A)>(), std::forward<B>(b)... ) )
  40. template<class... B> BOOST_CXX14_CONSTEXPR auto operator()( B&&... b ) &&
  41. BOOST_COMPAT_RETURNS( detail::invoke_bind_front_( std::move(f_), std::move(a_), make_index_sequence<sizeof...(A)>(), std::forward<B>(b)... ) )
  42. template<class... B> constexpr auto operator()( B&&... b ) const &&
  43. BOOST_COMPAT_RETURNS( detail::invoke_bind_front_( std::move(f_), std::move(a_), make_index_sequence<sizeof...(A)>(), std::forward<B>(b)... ) )
  44. };
  45. } // namespace detail
  46. template<class F, class... A> constexpr auto bind_front( F&& f, A&&... a ) -> detail::bind_front_< decay_t<F>, decay_t<A>... >
  47. {
  48. return { std::forward<F>(f), std::forward<A>(a)... };
  49. }
  50. } // namespace compat
  51. } // namespace boost
  52. #endif // BOOST_COMPAT_BIND_FRONT_HPP_INCLUDED