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
46namespace std _GLIBCXX_VISIBILITY(default)
47{
48 /// initializer_list
49 template<class _E>
50 // _GLIBCXX_RESOLVE_LIB_DEFECTS
51 // 2129. User specializations of std::initializer_list
52 class _GLIBCXX_NO_SPECIALIZATIONS initializer_list
53 {
54 public:
55 typedef _E value_type;
56 typedef const _E& reference;
57 typedef const _E& const_reference;
58 typedef size_t size_type;
59 typedef const _E* iterator;
60 typedef const _E* const_iterator;
61
62 private:
63 iterator _M_array;
64 size_type _M_len;
65
66 // The compiler can call a private constructor.
67 constexpr initializer_list(const_iterator __a, size_type __l)
68 : _M_array(__a), _M_len(__l) { }
69
70 public:
71 constexpr initializer_list() noexcept
72 : _M_array(0), _M_len(0) { }
73
74 // Number of elements.
75 constexpr size_type
76 size() const noexcept { return _M_len; }
77
78 // First element.
79 constexpr const_iterator
80 begin() const noexcept { return _M_array; }
81
82 // One past the last element.
83 constexpr const_iterator
84 end() const noexcept { return begin() + size(); }
85
86#if __glibcxx_initializer_list >= 202511L
87 constexpr bool
88 empty() const noexcept { return _M_len == 0; }
89
90 constexpr const value_type*
91 data() const noexcept { return _M_array; }
92#endif
93 };
94
95#if __glibcxx_initializer_list < 202511L
96 /**
97 * @brief Return an iterator pointing to the first element of
98 * the initializer_list.
99 * @param __ils Initializer list.
100 * @relates initializer_list
101 */
102 template<class _Tp>
103 constexpr const _Tp*
104 begin(initializer_list<_Tp> __ils) noexcept
105 { return __ils.begin(); }
106
107 /**
108 * @brief Return an iterator pointing to one past the last element
109 * of the initializer_list.
110 * @param __ils Initializer list.
111 * @relates initializer_list
112 */
113 template<class _Tp>
114 constexpr const _Tp*
115 end(initializer_list<_Tp> __ils) noexcept
116 { return __ils.end(); }
117#endif // __glibcxx_initializer_list < 202511L
118}
119
120#endif // C++11
121
122#endif // _INITIALIZER_LIST
ISO C++ entities toplevel namespace is std.
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.
constexpr auto begin(_Container &__cont) noexcept(noexcept(__cont.begin())) -> decltype(__cont.begin())
Return an iterator pointing to the first element of the container.
initializer_list