std::istrea...eam_iterator(3)

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

✨ NAME ✨

std::istream_iterator::istream_iterator β€” the constructor for std::istream_iterator, aka the glow-up moment where your iterator gets its act together πŸ’…


🌸 Synopsis 🌸

istream_iterator();                                      (until C++11)
constexpr istream_iterator();                            (since C++11)
constexpr istream_iterator(                          (2) (since C++20)
std::default_sentinel_t );
istream_iterator( istream_type& stream );        (1) (3)
istream_iterator( const istream_iterator& other
(until C++11)
);                                                   (4)
istream_iterator( const istream_iterator& other
(since C++11)
) = default;

πŸŽ€ Description πŸŽ€

okay so here's the tea on each overload, bestie β˜•οΈ

  • 1,2) β€” constructs the end-of-stream iterator, and value-initializes the stored value. This constructor is constexpr if the initializer in the definition auto x = T(); is a constant initializer (since C++11). no cap, it's giving "I'm literally the end, periodt" πŸ’€
  • 3) β€” initializes the iterator, stores the address of stream in a data member, and performs the first read from the input stream to initialize the cached value data member. it's the main character constructor fr fr πŸ”₯
  • 4) β€” constructs a copy of other. If std::is_trivially_copy_constructible<T>::value is true, this copy constructor is a trivial copy constructor (since C++11). slay, lowkey efficient πŸ¦‹

🧚 Parameters 🧚

  • stream β€” stream to initialize the istream_iterator with. the source of the vibes, iykyk 🌈
  • other β€” another istream_iterator of the same type. your iterator bestie 🫢

πŸ’– Example πŸ’–

// Run this code

#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>

int main()
{
    std::istringstream stream("1 2 3 4 5");
    std::copy(
        std::istream_iterator<int>(stream),
        std::istream_iterator<int>(),
        std::ostream_iterator<int>(std::cout, " ")
    );
}

Output:

1 2 3 4 5

literally iconic output, 10/10, ate and left no crumbs πŸ‘‘


⭐ Defect reports ⭐

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. not me reading the patch notes like it's drama πŸ’€

  • LWG 1280 β€” applied to C++98 β€” behavior as published: overload (3) stored stream directly β€” correct behavior: stores its address instead 🌸
  • P0738R2 β€” applied to C++98 β€” behavior as published: the first read might be deferred to the first dereference β€” correct behavior: always performed in the constructor πŸ”₯

bestie if you made it this far you're officially a C++ scholar, no cap πŸŽ“πŸ’…

source: http://cppreference.com Β· version: 2024.06.10 Β· that's the whole std::istream_iterator(3) moment, periodt πŸ¦‹βœ¨

πŸ“¦ where this page comes from