public member function
<initializer_list>

std::initializer_list::size

size_t size() const noexcept;
Return size of list
Returns the number of elements in the initializer_list.

Parameters

none

Return Value

The number of elements in the list.

size_t is an unsigned integral type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// initializer_list::size
#include <iostream>          // std::cout
#include <initializer_list>  // std::initializer_list

template<class T> struct simple_container {
  T * data;
  unsigned n;
  simple_container(std::initializer_list<int> args) {
    data = new T [args.size()];
    n=0;
    for (T x : args) {data[n++]=x;}
  }
  T* begin() {return data;}
  T* end() {return data+n;}
};

int main ()
{
  simple_container<int> myobject {10, 20, 30};
  std::cout << "myobject contains:";
  for (int x : myobject) std::cout << ' ' << x;
  std::cout << '\n';
  return 0;
}


Output:
myobject contains: 10 20 30

Complexity

Constant.

Data races

The object is accessed. All contained elements are constant: Concurrently accessing them is always safe.

Exception safety

No-throw guarantee: this member function never throws exceptions.

See also