1 /*! 2 @file 3 Defines `boost::hana::back`. 4 5 @copyright Louis Dionne 2013-2017 6 Distributed under the Boost Software License, Version 1.0. 7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) 8 */ 9 10 #ifndef BOOST_HANA_BACK_HPP 11 #define BOOST_HANA_BACK_HPP 12 13 #include <boost/hana/fwd/back.hpp> 14 15 #include <boost/hana/at.hpp> 16 #include <boost/hana/concept/iterable.hpp> 17 #include <boost/hana/config.hpp> 18 #include <boost/hana/core/dispatch.hpp> 19 #include <boost/hana/length.hpp> 20 21 #include <cstddef> 22 23 24 BOOST_HANA_NAMESPACE_BEGIN 25 //! @cond 26 template <typename Xs> operator ()(Xs && xs) const27 constexpr decltype(auto) back_t::operator()(Xs&& xs) const { 28 using It = typename hana::tag_of<Xs>::type; 29 using Back = BOOST_HANA_DISPATCH_IF(back_impl<It>, 30 hana::Iterable<It>::value 31 ); 32 33 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS 34 static_assert(hana::Iterable<It>::value, 35 "hana::back(xs) requires 'xs' to be an Iterable"); 36 #endif 37 38 return Back::apply(static_cast<Xs&&>(xs)); 39 } 40 //! @endcond 41 42 template <typename It, bool condition> 43 struct back_impl<It, when<condition>> : default_ { 44 template <typename Xs> applyback_impl45 static constexpr decltype(auto) apply(Xs&& xs) { 46 constexpr std::size_t len = decltype(hana::length(xs))::value; 47 static_assert(len > 0, "hana::back(xs) requires 'xs' to be non-empty"); 48 return hana::at_c<len - 1>(static_cast<Xs&&>(xs)); 49 } 50 }; 51 BOOST_HANA_NAMESPACE_END 52 53 #endif // !BOOST_HANA_BACK_HPP 54