Skip to content

std::initializer_list (C++11) ​

In a Nutshell ​

A lightweight, read-only proxy object that lets you conveniently pass any number of same-typed initial values to containers or custom classes using curly braces {}.

#include <initializer_list>

Core API Quick Reference ​

OperationSignatureDescription
Constructorinitializer_list() noexceptCreates an empty list (usually implicitly constructed by the compiler)
Element Countstd::size_t size() const noexceptReturns the number of elements in the list
Begin Pointerconst T* begin() const noexceptPointer to the first element
End Pointerconst T* end() const noexceptPointer to one past the last element
Begin Iteratorconst T* begin(std::initializer_list<T> il) noexceptOverload of std::begin
End Iteratorconst T* end(std::initializer_list<T> il) noexceptOverload of std::end

Minimal Example ​

C++
// Standard: C++11
#include <iostream>
#include <initializer_list>
#include <vector>

struct Container {
    std::vector<int> v;
    Container(std::initializer_list<int> l) : v(l) {}
    void append(std::initializer_list<int> l) {
        v.insert(v.end(), l.begin(), l.end());
    }
};

int main() {
    Container c = {1, 2, 3}; // implicitly constructs the initializer_list
    c.append({4, 5});
    for (int x : c.v) std::cout << x << ' ';
}

Embedded Applicability: High ​

  • The underlying implementation usually holds just a pointer and a length (or two pointers), so the memory overhead is minimal
  • Copying a std::initializer_list does not copy the underlying array — only the proxy object itself is copied, with no extra allocation cost
  • The underlying array may live in read-only memory, which makes it a good fit for initializing static configuration tables placed in ROM

Compiler Support ​

GCCClangMSVC
To be addedTo be addedTo be added

See Also ​


Some content references cppreference.com, used under the CC-BY-SA 4.0 license

pdf-latest-4-g85128cc · 85128cc · 2026-10-05