std::ios_base::Init(3)

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

πŸ’– NAME πŸ’–

std::ios_base::Init - std::ios_base::Init

yo bestie, welcome to the std::ios_base::Init(3) page from the C++ Standard Libary manual. The name is literally the same thing twice, and honestly? I respect the commitment.

✨ Synopsis ✨

class Init;

okay so here's the tea: this class is used to ensure that the default C++ streams (std::cin, std::cout, etc.) are properly initialized and destructed. she tracks how many instances of herself are created β€” when the first instance is constructed, she initializes the C++ streams, and when the last instance is destructed, she flushes the output streams. periodt.

The header <iostream> behaves as if it defines (directly or indirectly) an instance of std::ios_base::Init with static storage duration: this makes it safe to access the standard I/O streams in the constructors and destructors of static objects with ordered initialization (as long as <iostream> is included in the translation unit before these objects were defined). that's the safety net, bestie.

Each C++ library module in a hosted implementation behaves as if it contains an interface unit that defines an unexported std::ios_base::Init variable with ordered initialization. (since C++23)

As a result, the definition of that variable is appearance-ordered before any declaration following the point of importation of a C++ library module. Whether such a definition exists is unobservable by a program that does not reference any of the standard iostream objects. sneaky, but valid.

🌸 Member functions 🌸

  • constructor β€” initializes the default C++ streams if they have not been created yet (public member function).
  • destructor β€” flushes the default C++ streams if *this is the last instance to be destroyed (public member function).

πŸ’€ Defect reports πŸ’€

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • DR: LWG 1123
    • Applied to: C++98
    • Behavior as published: the behaviors of the constructor and destructor depend on an exposition-only static data member init_cnt
    • Correct behavior: removed the dependency

πŸ¦‹ See also πŸ¦‹

  • cin β€” reads from the standard C input stream stdin
  • wcin β€” (global object)
  • cout β€” writes to the standard C output stream stdout
  • wcout β€” (global object)
  • cerr β€” writes to the standard C error stream stderr, unbuffered
  • wcerr β€” (global object)
  • clog β€” writes to the standard C error stream stderr
  • wclog β€” (global object)

and that's the whole page, bestie. source/version/date: http://cppreference.com, 2024.06.10, std::ios_base::Init(3). mwah πŸ’‹

πŸ“¦ where this page comes from