initializer_list 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef __GBLIBCPP_INITIALIZER_LIST__
  2. #define __GBLIBCPP_INITIALIZER_LIST__
  3. #include <cstddef>
  4. namespace std {
  5. #if defined(__GNUC__)
  6. template <typename T>
  7. class initializer_list {
  8. public:
  9. using value_type = T;
  10. using reference = const T&;
  11. using const_reference = const T&;
  12. using size_type = std::size_t;
  13. using iterator = const T*;
  14. using const_iterator = const T*;
  15. private:
  16. const T* __begin;
  17. std::size_t __size;
  18. public:
  19. constexpr initializer_list() noexcept
  20. : __begin { }, __size { } { };
  21. constexpr const T* begin() const noexcept
  22. { return __begin; }
  23. constexpr const T* end() const noexcept
  24. { return __begin + __size; }
  25. constexpr size_type size() const noexcept
  26. { return __size; }
  27. };
  28. #elif defined(__clang__)
  29. template <typename T>
  30. class initializer_list {
  31. public:
  32. using value_type = T;
  33. using reference = const T&;
  34. using const_reference = const T&;
  35. using size_type = std::size_t;
  36. using iterator = const T*;
  37. using const_iterator = const T*;
  38. private:
  39. const T* __begin;
  40. const T* __end;
  41. public:
  42. constexpr initializer_list() noexcept
  43. : __begin { }, __end { } { };
  44. constexpr const T* begin() const noexcept
  45. { return __begin; }
  46. constexpr const T* end() const noexcept
  47. { return __end; }
  48. constexpr size_type size() const noexcept
  49. { return static_cast<size_type>(__end - __begin); }
  50. };
  51. #elif defined(_MSC_VER)
  52. template <typename T>
  53. class initializer_list {
  54. public:
  55. using value_type = T;
  56. using reference = const T&;
  57. using const_reference = const T&;
  58. using size_type = std::size_t;
  59. using iterator = const T*;
  60. using const_iterator = const T*;
  61. private:
  62. const T* __begin;
  63. const T* __end;
  64. public:
  65. constexpr initializer_list() noexcept
  66. : __begin { }, __end { } { };
  67. constexpr const T* begin() const noexcept
  68. { return __begin; }
  69. constexpr const T* end() const noexcept
  70. { return __end; }
  71. constexpr size_type size() const noexcept
  72. { return static_cast<size_type>(__end - __begin); }
  73. };
  74. #else
  75. template <typename T>
  76. class initializer_list;
  77. #endif
  78. template <typename T>
  79. constexpr const T* begin(initializer_list<T> il) noexcept
  80. { return il.begin(); }
  81. template <typename T>
  82. constexpr const T* end(initializer_list<T> il) noexcept
  83. { return il.end(); }
  84. template <typename T>
  85. [[nodiscard]] constexpr bool empty(initializer_list<T> il) noexcept
  86. { return il.size() == 0; }
  87. template <typename T>
  88. constexpr const T* data(initializer_list<T> il) noexcept
  89. { return il.begin(); }
  90. // TODO: std::rbegin() and std::rend() for std::initializer_list
  91. } // namespace std
  92. #endif