std::initia...ializer_list(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 ranged for loop 🌈
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: that static constexpr line 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