/*============================================================================= Copyright (c) 2014 Paul Fultz II arg.h Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) ==============================================================================*/ #ifndef BOOST_HOF_GUARD_FUNCTION_ARGS_H #define BOOST_HOF_GUARD_FUNCTION_ARGS_H #include #include #include #include /// arg /// === /// /// Description /// ----------- /// /// The `arg` function returns a function object that returns the Nth argument /// passed to it. It actually starts at 1, so it is not the zero-based index /// of the argument. /// /// Synopsis /// -------- /// /// template /// constexpr auto arg(IntegralConstant); /// /// template /// constexpr auto arg_c(Ts&&...); /// /// /// Example /// ------- /// /// #include /// #include /// using namespace boost::hof; /// /// int main() { /// assert(arg(std::integral_constant())(1,2,3,4,5) == 3); /// } /// namespace boost { namespace hof { namespace detail { template struct perfect_ref { typedef T type; typedef typename std::remove_reference::type value_type; T&& value; constexpr perfect_ref(value_type& x) noexcept : value(BOOST_HOF_FORWARD(T)(x)) {} }; template struct ignore { template constexpr ignore(T&&...) noexcept {} }; template struct args_at { template constexpr auto operator()(ignore..., T x, Ts...) const BOOST_HOF_RETURNS(BOOST_HOF_FORWARD(typename T::type)(x.value)); }; template constexpr args_at make_args_at(seq) noexcept { return {}; } template constexpr auto get_args(Ts&&... xs) BOOST_HOF_RETURNS ( boost::hof::detail::make_args_at(typename gens::type())(nullptr, BOOST_HOF_RETURNS_CONSTRUCT(perfect_ref)(xs)...) ); template struct make_args_f { template::type> constexpr auto operator()(Ts&&... xs) const BOOST_HOF_RETURNS ( boost::hof::detail::get_args(BOOST_HOF_FORWARD(Ts)(xs)...) ); }; struct arg_f { template constexpr make_args_f operator()(IntegralConstant) const noexcept { return make_args_f(); } }; } #if BOOST_HOF_HAS_VARIABLE_TEMPLATES template BOOST_HOF_STATIC_CONSTEXPR detail::make_args_f arg_c = {}; #else template constexpr auto arg_c(Ts&&... xs) BOOST_HOF_RETURNS ( boost::hof::detail::get_args(BOOST_HOF_FORWARD(Ts)(xs)...) ); #endif BOOST_HOF_DECLARE_STATIC_VAR(arg, detail::arg_f); }} // namespace boost::hof #endif