identity.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___FUNCTIONAL_IDENTITY_H
  10. #define _LIBCPP___FUNCTIONAL_IDENTITY_H
  11. #include <__config>
  12. #include <__fwd/functional.h>
  13. #include <__type_traits/integral_constant.h>
  14. #include <__utility/forward.h>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_BEGIN_NAMESPACE_STD
  19. template <class _Tp>
  20. struct __is_identity : false_type {};
  21. struct __identity {
  22. template <class _Tp>
  23. _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp&& operator()(_Tp&& __t) const _NOEXCEPT {
  24. return std::forward<_Tp>(__t);
  25. }
  26. using is_transparent = void;
  27. };
  28. template <>
  29. struct __is_identity<__identity> : true_type {};
  30. template <>
  31. struct __is_identity<reference_wrapper<__identity> > : true_type {};
  32. template <>
  33. struct __is_identity<reference_wrapper<const __identity> > : true_type {};
  34. #if _LIBCPP_STD_VER >= 20
  35. struct identity {
  36. template <class _Tp>
  37. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator()(_Tp&& __t) const noexcept {
  38. return std::forward<_Tp>(__t);
  39. }
  40. using is_transparent = void;
  41. };
  42. template <>
  43. struct __is_identity<identity> : true_type {};
  44. template <>
  45. struct __is_identity<reference_wrapper<identity> > : true_type {};
  46. template <>
  47. struct __is_identity<reference_wrapper<const identity> > : true_type {};
  48. #endif // _LIBCPP_STD_VER >= 20
  49. _LIBCPP_END_NAMESPACE_STD
  50. #endif // _LIBCPP___FUNCTIONAL_IDENTITY_H