tuple_element.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___TUPLE_TUPLE_ELEMENT_H
  9. #define _LIBCPP___TUPLE_TUPLE_ELEMENT_H
  10. #include <__config>
  11. #include <__tuple/tuple_indices.h>
  12. #include <__tuple/tuple_types.h>
  13. #include <cstddef>
  14. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  15. # pragma GCC system_header
  16. #endif
  17. _LIBCPP_BEGIN_NAMESPACE_STD
  18. template <size_t _Ip, class _Tp>
  19. struct _LIBCPP_TEMPLATE_VIS tuple_element;
  20. template <size_t _Ip, class _Tp>
  21. struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, const _Tp> {
  22. typedef _LIBCPP_NODEBUG const typename tuple_element<_Ip, _Tp>::type type;
  23. };
  24. template <size_t _Ip, class _Tp>
  25. struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, volatile _Tp> {
  26. typedef _LIBCPP_NODEBUG volatile typename tuple_element<_Ip, _Tp>::type type;
  27. };
  28. template <size_t _Ip, class _Tp>
  29. struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, const volatile _Tp> {
  30. typedef _LIBCPP_NODEBUG const volatile typename tuple_element<_Ip, _Tp>::type type;
  31. };
  32. #ifndef _LIBCPP_CXX03_LANG
  33. # if !__has_builtin(__type_pack_element)
  34. namespace __indexer_detail {
  35. template <size_t _Idx, class _Tp>
  36. struct __indexed {
  37. using type _LIBCPP_NODEBUG = _Tp;
  38. };
  39. template <class _Types, class _Indexes>
  40. struct __indexer;
  41. template <class... _Types, size_t... _Idx>
  42. struct __indexer<__tuple_types<_Types...>, __tuple_indices<_Idx...>> : __indexed<_Idx, _Types>... {};
  43. template <size_t _Idx, class _Tp>
  44. __indexed<_Idx, _Tp> __at_index(__indexed<_Idx, _Tp> const&);
  45. } // namespace __indexer_detail
  46. # if !defined(__CUDACC__) || !defined(_MSC_VER)
  47. template <size_t _Idx, class... _Types>
  48. using __type_pack_element _LIBCPP_NODEBUG = typename decltype(__indexer_detail::__at_index<_Idx>(
  49. __indexer_detail::__indexer< __tuple_types<_Types...>,
  50. typename __make_tuple_indices<sizeof...(_Types)>::type >{}))::type;
  51. # else // !defined(__CUDACC__) || !defined(_MSC_VER)
  52. template <size_t _Idx, class... _Types>
  53. struct __y_type_pack_element {
  54. using __t1 = typename __make_tuple_indices<sizeof...(_Types)>::type;
  55. using __t2 = __indexer_detail::__indexer<__tuple_types<_Types...>, __t1>;
  56. using __t3 = decltype(__indexer_detail::__at_index<_Idx>(__t2{}));
  57. using __t4 = typename __t3::type;
  58. };
  59. template <size_t _Idx, class... _Types>
  60. using __type_pack_element = typename __y_type_pack_element<_Idx, _Types...>::__t4;
  61. # endif // !defined(__CUDACC__) || !defined(_MSC_VER)
  62. # endif // __has_builtin(__type_pack_element)
  63. template <size_t _Ip, class... _Types>
  64. struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, __tuple_types<_Types...> > {
  65. static_assert(_Ip < sizeof...(_Types), "tuple_element index out of range");
  66. typedef _LIBCPP_NODEBUG __type_pack_element<_Ip, _Types...> type;
  67. };
  68. # if _LIBCPP_STD_VER >= 14
  69. template <size_t _Ip, class... _Tp>
  70. using tuple_element_t _LIBCPP_NODEBUG = typename tuple_element<_Ip, _Tp...>::type;
  71. # endif
  72. #endif // _LIBCPP_CXX03_LANG
  73. _LIBCPP_END_NAMESPACE_STD
  74. #endif // _LIBCPP___TUPLE_TUPLE_ELEMENT_H