std::initia...ializer_list(3)
π NAME π
std::initializer_list::initializer_list β okay so this is literally the constructor page for std::initializer_list::initializer_list, bestie. That's it, that's the whole name. We're starting strong. β¨
This is a page from the C++ Standard Library manual, section 3, and it's here to explain the one and only constructor of std::initializer_list. Buckle up, it's short but it's iconic. π
πΈ SYNOPSIS πΈ
initializer_list() noexcept; (since C++11)
(until C++14)
constexpr initializer_list() noexcept; (since C++14)
So what does it do? It constructs an empty initializer list. That's the whole vibe. An empty one. Zero elements. A little empty vessel waiting for its moment. π«Ά
Notice the glow-up between C++11 and C++14: it picked up constexpr, so from C++14 onward it can be evaluated at compile time. Highkey a main character evolution. π
π¦ PARAMETERS π¦
(none) β literally no parameters. Zero. Not a single one. She takes nothing and she gives you an empty list. Balance. π§
β COMPLEXITY β
Constant. Every single time. No loops, no searching, no drama β constant complexity, periodt. π
π₯ NOTES π₯
Okay this is the part where it gets interesting, so pay attention bestie:
Despite a lack of constructors, it is possible to create non-empty initializer lists. Yes you read that right β no visible constructors, and yet non-empty lists exist. The audacity. π
Instances of std::initializer_list are implicitly constructed when:
- a braced-init-list is used in list-initialization, including function-call list initialization and assignment expressions β and ngl, do not confuse this with constructor initializer lists, they are a whole different thing, iykyk π
- a braced-init-list is bound to
auto, including in a rangedforloop π
No cap: those two bullets are the entire magic trick. That's where your non-empty lists come from. Memorize them and you're slaying the vibe check. π
π EXAMPLE π
Run this code, bestie, it's cute:
// Run this code
#include <iostream>
#include <initializer_list>
int main()
{
std::initializer_list<int> empty_list;
std::cout << "empty_list.size(): " << empty_list.size() << '\n';
// create initializer lists using list-initialization
std::initializer_list<int> digits {1, 2, 3, 4, 5};
std::cout << "digits.size(): " << digits.size() << '\n';
// special rule for auto means 'fractions' has the
// type std::initializer_list<double>
auto fractions = {3.14159, 2.71828};
std::cout << "fractions.size(): " << fractions.size() << '\n';
// create constexpr initializer list (since C++14)
static constexpr auto ab = {'a', 'b'};
static_assert(ab.size() == 2 and *ab.begin() == 'a');
}
Look at her go β one default-constructed empty list, one list-initialized list of digits, one auto-deduced list of fractions (special rule for auto, remember? it gets the type std::initializer_list<double> β¨), and one static constexpr list because C++14 said "let her be constexpr." The static_assert isn't even a runtime thing β it's checked at compile time. Obsessed. π
π OUTPUT π
empty_list.size(): 0
digits.size(): 5
fractions.size(): 2
Three lines. Zero surprises. The empty one is 0, exactly like we promised. Ate and left no crumbs. π
Heads up: thatstatic constexprline is marked since C++14 in the example comment β it's not available if you're compiling under an older standard. Compile with the right-std=flag and she'll behave. π
and that's the whole page, bestie π stay constexpr, stay noexcept β this cute lil sign-off comes to you from cppreference.com, version dated 2024.06.10, filed under std::initia...ializer_list(3) because the title got truncated and honestly? relatable. πβ¨π¦
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms