| 1 | /*! |
| 2 | @file |
| 3 | Defines `boost::hana::always`. |
| 4 | |
| 5 | Copyright Louis Dionne 2013-2022 |
| 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_FUNCTIONAL_ALWAYS_HPP |
| 11 | #define BOOST_HANA_FUNCTIONAL_ALWAYS_HPP |
| 12 | |
| 13 | #include <boost/hana/config.hpp> |
| 14 | #include <boost/hana/detail/create.hpp> |
| 15 | |
| 16 | #include <utility> |
| 17 | |
| 18 | |
| 19 | namespace boost { namespace hana { |
| 20 | //! @ingroup group-functional |
| 21 | //! Return a constant function returning `x` regardless of the |
| 22 | //! argument(s) it is invoked with. |
| 23 | //! |
| 24 | //! Specifically, `always(x)` is a function such that |
| 25 | //! @code |
| 26 | //! always(x)(y...) == x |
| 27 | //! @endcode |
| 28 | //! for any `y...`. A copy of `x` is made and it is owned by the |
| 29 | //! `always(x)` function. When `always(x)` is called, it will return |
| 30 | //! a reference to the `x` it owns. This reference is valid as long |
| 31 | //! as `always(x)` is in scope. |
| 32 | //! |
| 33 | //! |
| 34 | //! ### Example |
| 35 | //! @include example/functional/always.cpp |
| 36 | #ifdef BOOST_HANA_DOXYGEN_INVOKED |
| 37 | constexpr auto always = [](auto&& x) { |
| 38 | return [perfect-capture](auto const& ...y) -> decltype(auto) { |
| 39 | return forwarded(x); |
| 40 | }; |
| 41 | }; |
| 42 | #else |
| 43 | template <typename T> |
| 44 | struct _always { |
| 45 | T val_; |
| 46 | |
| 47 | template <typename ...Args> |
| 48 | constexpr T const& operator()(Args const& ...) const& |
| 49 | { return val_; } |
| 50 | |
| 51 | template <typename ...Args> |
| 52 | constexpr T& operator()(Args const& ...) & |
| 53 | { return val_; } |
| 54 | |
| 55 | template <typename ...Args> |
| 56 | constexpr T operator()(Args const& ...) && |
| 57 | { return std::move(val_); } |
| 58 | }; |
| 59 | |
| 60 | BOOST_HANA_INLINE_VARIABLE constexpr detail::create<_always> always{}; |
| 61 | #endif |
| 62 | }} // end namespace boost::hana |
| 63 | |
| 64 | #endif // !BOOST_HANA_FUNCTIONAL_ALWAYS_HPP |
| 65 | |