22// Initializer list
33//----------------------------------------------------------------------------------------------------------------------
44#pragma once
5- #ifndef _INITIALIZER_LIST_
6- #define _INITIALIZER_LIST_
7-
8- namespace std {
9-
10- template<class E> class initializer_list {
11- public: // Nested types
12- using value_type = E;
13- using reference = const E&;
14- using const_reference = const E&;
15- using size_type = size_t;
16- using iterator = const E*;
17- using const_iterator = const E*;
18-
19- public: // Interface
20- constexpr initializer_list() noexcept;
21- constexpr size_t size() const noexcept { return _end - _start; } ///< number of elements
22- constexpr const E* begin() const noexcept { return _start; } ///< first element
23- constexpr const E* end() const noexcept { return _end; } ///< one past the last element
24-
25- private:
26- E* _start = nullptr;
27- E* _end = nullptr;
28- }; // Initializer list range access
29- template<class E> constexpr const E* begin(initializer_list<E> il) noexcept {
30- return il.begin();
31- }
32- template<class E> constexpr const E* end(initializer_list<E> il) noexcept {
33- return il.end();
34- }
35- }
36-
37- #endif // _INITIALIZER_LIST_
5+
6+ namespace std
7+ {
8+ /// initializer_list
9+ template<class _E>
10+ class initializer_list
11+ {
12+ public:
13+ typedef _E value_type;
14+ typedef const _E& reference;
15+ typedef const _E& const_reference;
16+ typedef size_t size_type;
17+ typedef const _E* iterator;
18+ typedef const _E* const_iterator;
19+
20+ private:
21+ iterator _M_array;
22+ size_type _M_len;
23+
24+ // The compiler can call a private constructor.
25+ constexpr initializer_list(const_iterator __a, size_type __l)
26+ : _M_array(__a), _M_len(__l) { }
27+
28+ public:
29+ constexpr initializer_list() noexcept
30+ : _M_array(0), _M_len(0) { }
31+
32+ // Number of elements.
33+ constexpr size_type
34+ size() const noexcept { return _M_len; }
35+
36+ // First element.
37+ constexpr const_iterator
38+ begin() const noexcept { return _M_array; }
39+
40+ // One past the last element.
41+ constexpr const_iterator
42+ end() const noexcept { return begin() + size(); }
43+ };
44+
45+ /**
46+ * @brief Return an iterator pointing to the first element of
47+ * the initializer_list.
48+ * @param __ils Initializer list.
49+ */
50+ template<class _Tp>
51+ constexpr const _Tp*
52+ begin(initializer_list<_Tp> __ils) noexcept
53+ { return __ils.begin(); }
54+
55+ /**
56+ * @brief Return an iterator pointing to one past the last element
57+ * of the initializer_list.
58+ * @param __ils Initializer list.
59+ */
60+ template<class _Tp>
61+ constexpr const _Tp*
62+ end(initializer_list<_Tp> __ils) noexcept
63+ { return __ils.end(); }
64+ }
0 commit comments