initializer_list 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // std::initializer_list support -*- C++ -*-
  2. // Copyright (C) 2008-2021 Free Software Foundation, Inc.
  3. //
  4. // This file is part of GCC.
  5. //
  6. // GCC is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation; either version 3, or (at your option)
  9. // any later version.
  10. //
  11. // GCC is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // Under Section 7 of GPL version 3, you are granted additional
  17. // permissions described in the GCC Runtime Library Exception, version
  18. // 3.1, as published by the Free Software Foundation.
  19. // You should have received a copy of the GNU General Public License and
  20. // a copy of the GCC Runtime Library Exception along with this program;
  21. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  22. // <http://www.gnu.org/licenses/>.
  23. /** @file initializer_list
  24. * This is a Standard C++ Library header.
  25. */
  26. #ifndef _INITIALIZER_LIST
  27. #define _INITIALIZER_LIST
  28. #pragma GCC system_header
  29. #if __cplusplus < 201103L
  30. # include <bits/c++0x_warning.h>
  31. #else // C++0x
  32. #pragma GCC visibility push(default)
  33. #include <bits/c++config.h>
  34. namespace std
  35. {
  36. /// initializer_list
  37. template<class _E>
  38. class initializer_list
  39. {
  40. public:
  41. typedef _E value_type;
  42. typedef const _E& reference;
  43. typedef const _E& const_reference;
  44. typedef size_t size_type;
  45. typedef const _E* iterator;
  46. typedef const _E* const_iterator;
  47. private:
  48. iterator _M_array;
  49. size_type _M_len;
  50. // The compiler can call a private constructor.
  51. constexpr initializer_list(const_iterator __a, size_type __l)
  52. : _M_array(__a), _M_len(__l) { }
  53. public:
  54. constexpr initializer_list() noexcept
  55. : _M_array(0), _M_len(0) { }
  56. // Number of elements.
  57. constexpr size_type
  58. size() const noexcept { return _M_len; }
  59. // First element.
  60. constexpr const_iterator
  61. begin() const noexcept { return _M_array; }
  62. // One past the last element.
  63. constexpr const_iterator
  64. end() const noexcept { return begin() + size(); }
  65. };
  66. /**
  67. * @brief Return an iterator pointing to the first element of
  68. * the initializer_list.
  69. * @param __ils Initializer list.
  70. * @relates initializer_list
  71. */
  72. template<class _Tp>
  73. constexpr const _Tp*
  74. begin(initializer_list<_Tp> __ils) noexcept
  75. { return __ils.begin(); }
  76. /**
  77. * @brief Return an iterator pointing to one past the last element
  78. * of the initializer_list.
  79. * @param __ils Initializer list.
  80. * @relates initializer_list
  81. */
  82. template<class _Tp>
  83. constexpr const _Tp*
  84. end(initializer_list<_Tp> __ils) noexcept
  85. { return __ils.end(); }
  86. }
  87. #pragma GCC visibility pop
  88. #endif // C++11
  89. #endif // _INITIALIZER_LIST