identity.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*=============================================================================
  2. Copyright (c) 2012 Paul Fultz II
  3. identity.h
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #ifndef BOOST_HOF_GUARD_FUNCTION_IDENTITY_H
  8. #define BOOST_HOF_GUARD_FUNCTION_IDENTITY_H
  9. /// identity
  10. /// ========
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `identity` function is an unary function object that returns whats given to it.
  16. ///
  17. /// Semantics
  18. /// ---------
  19. ///
  20. /// assert(identity(x) == x);
  21. ///
  22. /// Synopsis
  23. /// --------
  24. ///
  25. /// template<class T>
  26. /// constexpr T identity(T&& x);
  27. ///
  28. #include <utility>
  29. #include <initializer_list>
  30. #include <boost/hof/detail/forward.hpp>
  31. #include <boost/hof/detail/static_const_var.hpp>
  32. namespace boost { namespace hof { namespace identity_detail {
  33. struct identity_base
  34. {
  35. template<class T>
  36. constexpr T operator()(T&& x) const
  37. noexcept(std::is_reference<T>::value || BOOST_HOF_IS_NOTHROW_MOVE_CONSTRUCTIBLE(T))
  38. {
  39. return BOOST_HOF_FORWARD(T)(x);
  40. }
  41. template<class T>
  42. constexpr std::initializer_list<T>& operator()(std::initializer_list<T>& x) const noexcept
  43. {
  44. return x;
  45. }
  46. template<class T>
  47. constexpr const std::initializer_list<T>& operator()(const std::initializer_list<T>& x) const noexcept
  48. {
  49. return x;
  50. }
  51. template<class T>
  52. constexpr std::initializer_list<T> operator()(std::initializer_list<T>&& x) const noexcept(noexcept(std::initializer_list<T>(std::move(x))))
  53. {
  54. return BOOST_HOF_FORWARD(std::initializer_list<T>)(x);
  55. }
  56. };
  57. }
  58. BOOST_HOF_DECLARE_STATIC_VAR(identity, identity_detail::identity_base);
  59. }} // namespace boost::hof
  60. #endif