FastArduino  v1.8
C++ library to build fast but small Arduino/AVR projects
initializer_list.h
1 // Copyright 2016-2021 Jean-Francois Poilpret
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 #ifndef INITIALIZER_LIST_HH
17 #define INITIALIZER_LIST_HH
18 
19 #include <stddef.h>
20 
28 namespace std
29 {
56  template<class T> class initializer_list
57  {
58  private:
59  const T* array_ = nullptr;
60  size_t len_ = 0;
61 
62  // The compiler can (and will) call a private constructor.
63  // This constructor will get called when the compiler encounters a braced
64  // list expression with elements of type T, like in f({t1, t2, t3}) and
65  // an initializer_list<T> is expected by f function.
66  constexpr initializer_list(const T* array, size_t len) : array_{array}, len_{len} {}
67 
68  public:
70  constexpr initializer_list() = default;
71 
73  constexpr size_t size() const
74  {
75  return len_;
76  }
77 
79  constexpr const T* begin() const
80  {
81  return array_;
82  }
83 
85  constexpr const T* end() const
86  {
87  return begin() + size();
88  }
89  };
90 }
91 
92 #endif /* INITIALIZER_LIST_HH */
93 
std::initializer_list::size
constexpr size_t size() const
The size of this initializer_list.
Definition: initializer_list.h:73
std::initializer_list::begin
constexpr const T * begin() const
The first element of this initializer_list.
Definition: initializer_list.h:79
std::initializer_list::initializer_list
constexpr initializer_list()=default
Create an empty initializer list.
std
Similar to standard C++ std namespace, this namespace is used by FastArduino library to implement var...
Definition: initializer_list.h:29
std::initializer_list::end
constexpr const T * end() const
One past the last element of this initializer_list.
Definition: initializer_list.h:85
std::initializer_list
C++ standard support for "initializer_list".
Definition: initializer_list.h:57