libstdc++: initializer_list Source File

libstdc++

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