Skip to main content

Defined in: <initializer_list>

Overview

template< class T >
class initializer_list;

std::initializer_list is a light container that holds objects from list initialization or braced initialization.

Memory

important

This section requires improvement. You can help by editing this doc page.

Technical details

Feature testing macros
important

This section requires improvement. You can help by editing this doc page.

Technical definition

An object of type std::initializer_list<T> is a lightweight proxy object that provides access to an array of objects of type const T.

A std::initializer_list<T> object is automatically constructed when:

  • A braced-init-list is used to list-initialize an object, where the corresponding constructor accepts an std::initializer_list<T> parameter
  • A braced-init-list is used as the right operand of assignment or as a function call argument, and the corresponding assignment operator/function accepts an std::initializer_list<T> parameter
  • A braced-init-list is bound to auto, including in a ranged for loop

Initializer lists may be implemented as a pair of pointers or pointer and length.

note

Copying a std::initializer_list does not copy the underlying objects.

The underlying array is a temporary array of type const T[N], in which each element is copy-initialized (except that narrowing conversions are invalid) from the corresponding element of the original initializer list. The lifetime of the underlying array is the same as any other temporary object, except that initializing an initializer list object from the array extends the lifetime of the array exactly like binding a reference to a temporary (with the same exceptions, such as for initializing a non-static class member). The underlying array may be allocated in read-only memory.

danger

The program is ill-formed

if an explicit or partial specialization of std::initializer_list is declared.

std::initializer_list

Defined ininitializer_list

Template parameters

pubTType of the elements.

Type names

pubvalue_typeT
pubreferenceconst T&
pubconst_referenceconst T&
pubsize_typeUnsigned integer type (usually std::size_t)
pubiteratorconst T*
pubconst_iteratorconst T*

Member functions

pub(constructors)

Constructs a initializer list.

pub(destructor)

Destroys the initializer list.

Capacity

pubsize

Returns the number of elements.

Iterators

pub begin
cbegin  (since C++11)

Returns an iterator/const_iterator to the beginning.

pub end
cend  (since C++11)

Returns an iterator/const_iterator to the end.

Non-member functions

pubstd::begin (std::initializer_list)

An overload for a std::begin.

pubstd::end (std::initializer_list)

An overload for a std::end.

Free function templates overloaded for std::initializer_list

pub rbegin
crbegin  (since C++14)

Returns a reverse iterator/const_iterator to the beginning.

pub rend
crend  (since C++14)

Returns a reverse iterator/const_iterator to the end.

pub empty  (since C++17)

Returns true if the initializer list is empty, otherwise false.

pub data  (since C++17)

Returns a pointer to the first element of the underlying array.

Examples

important

This section requires improvement. You can help by editing this doc page.

This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

Defined in: <initializer_list>

Overview

template< class T >
class initializer_list;

std::initializer_list is a light container that holds objects from list initialization or braced initialization.

Memory

important

This section requires improvement. You can help by editing this doc page.

Technical details

Feature testing macros
important

This section requires improvement. You can help by editing this doc page.

Technical definition

An object of type std::initializer_list<T> is a lightweight proxy object that provides access to an array of objects of type const T.

A std::initializer_list<T> object is automatically constructed when:

  • A braced-init-list is used to list-initialize an object, where the corresponding constructor accepts an std::initializer_list<T> parameter
  • A braced-init-list is used as the right operand of assignment or as a function call argument, and the corresponding assignment operator/function accepts an std::initializer_list<T> parameter
  • A braced-init-list is bound to auto, including in a ranged for loop

Initializer lists may be implemented as a pair of pointers or pointer and length.

note

Copying a std::initializer_list does not copy the underlying objects.

The underlying array is a temporary array of type const T[N], in which each element is copy-initialized (except that narrowing conversions are invalid) from the corresponding element of the original initializer list. The lifetime of the underlying array is the same as any other temporary object, except that initializing an initializer list object from the array extends the lifetime of the array exactly like binding a reference to a temporary (with the same exceptions, such as for initializing a non-static class member). The underlying array may be allocated in read-only memory.

danger

The program is ill-formed

if an explicit or partial specialization of std::initializer_list is declared.

std::initializer_list

Defined ininitializer_list

Template parameters

pubTType of the elements.

Type names

pubvalue_typeT
pubreferenceconst T&
pubconst_referenceconst T&
pubsize_typeUnsigned integer type (usually std::size_t)
pubiteratorconst T*
pubconst_iteratorconst T*

Member functions

pub(constructors)

Constructs a initializer list.

pub(destructor)

Destroys the initializer list.

Capacity

pubsize

Returns the number of elements.

Iterators

pub begin
cbegin  (since C++11)

Returns an iterator/const_iterator to the beginning.

pub end
cend  (since C++11)

Returns an iterator/const_iterator to the end.

Non-member functions

pubstd::begin (std::initializer_list)

An overload for a std::begin.

pubstd::end (std::initializer_list)

An overload for a std::end.

Free function templates overloaded for std::initializer_list

pub rbegin
crbegin  (since C++14)

Returns a reverse iterator/const_iterator to the beginning.

pub rend
crend  (since C++14)

Returns a reverse iterator/const_iterator to the end.

pub empty  (since C++17)

Returns true if the initializer list is empty, otherwise false.

pub data  (since C++17)

Returns a pointer to the first element of the underlying array.

Examples

important

This section requires improvement. You can help by editing this doc page.

This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.