std::ostrstream(3)

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

πŸ‘‘ std::ostrstream πŸ‘‘ β€” like, the C++ Standard Library one, bestie (page std::ostrstream(3), C++ Standard Libary, std::ostrstream(3))

Ok so listen. This is std::ostrstream, and no I'm not gonna pretend it's still that girl β€” it's deprecated and honestly kind of a legacy icon. But we're gonna learn it anyway because your girl does not skip content. Periodt. πŸ’…


✨ NAME ✨

std::ostrstream β€” literally std::ostrstream. That's the name. That's the whole thing. πŸŽ€


πŸ¦‹ Synopsis πŸ¦‹

First of all, where does it live:

Defined in header <strstream>

And here's the class, exactly as it is:

class ostrstream : public std::ostream  (deprecated in C++98)
                                        (removed in C++26)

So real talk: the class ostrstream implements output operations on array-backed streams. Basically it wraps a raw array I/O device implementation (std::strstreambuf) into the higher-level interface of std::basic_ostream. It's giving "glow-up wrapper" energy ✨

And the typical implementation of ostrstream holds only one non-derived data member: an object of type std::strstreambuf. One. Single. Member. Minimalist queen 🫢


🌸 Notes 🌸

Okay these notes are important, do not scroll past:

  • After any call to str(), a call to freeze(false) is required to allow the destructor to deallocate the buffer as necessary. So like, don't forget the freeze(false) or your destructor can't do its job 😭
  • Before any call to str() that uses the result as a C-string, the buffer must be null-terminated, typically with std::ends. Get that terminating vibe in there first, iykyk.
  • ostrstream has been deprecated since C++98 and removed since C++26. Yeah. It's been over for a minute. πŸ’€
  • The recommended replacements are std::ostringstream, std::ospanstream (since C++23), and boost::iostreams::array_sink. Those are the ones eating in 2023+ and beyond. πŸ“
Bestie note: the "deprecated since C++98, removed since C++26" thing is not a drill. If you're reaching for this in new code, maybe… don't? The replacements above are right there.

πŸ”₯ Member functions πŸ”₯

The main character lineup, all public members:

  • constructor β€” constructs an ostrstream object, optionally allocating the buffer (public member function)
  • destructor β€” destructs an ostrstream object, optionally deallocating the buffer, and it's marked [virtual] (virtual public member function)
  • rdbuf β€” obtains a pointer to the associated strstreambuf (public member function)
  • str β€” accesses the output buffer (public member function)
  • freeze β€” disables/enables automatic reallocation (public member function)
  • pcount β€” obtains the number of characters written (public member function)

πŸ’œ Inherited from std::basic_ostream πŸ’œ

Yeah she inherited all of this. Generous ancestor. 🌈

πŸŽ€ Member functions πŸŽ€

πŸ’– Formatted output

  • operator<< β€” inserts formatted data (public member function of std::basic_ostream<CharT,Traits>)

πŸ’« Unformatted output

  • put β€” inserts a character (public member function of std::basic_ostream<CharT,Traits>)
  • write β€” inserts blocks of characters (public member function of std::basic_ostream<CharT,Traits>)

⭐ Positioning

  • tellp β€” returns the output position indicator (public member function of std::basic_ostream<CharT,Traits>)
  • seekp β€” sets the output position indicator (public member function of std::basic_ostream<CharT,Traits>)

πŸ¦‹ Miscellaneous

  • flush β€” synchronizes with the underlying storage device (public member function of std::basic_ostream<CharT,Traits>)

πŸ‘‘ Member classes

  • sentry β€” implements basic logic for preparation of the stream for output operations (public member class of std::basic_ostream<CharT,Traits>)

🧚 Inherited from std::basic_ios 🧚

More inheritance, more slay. πŸŽ€

🌟 Member types

Here's the whole mapping, one bullet per type because tables are not the vibe in this terminal:

  • char_type β†’ CharT
  • traits_type β†’ Traits
  • int_type β†’ Traits::int_type
  • pos_type β†’ Traits::pos_type
  • off_type β†’ Traits::off_type

🌸 Member functions 🌸

πŸ” State functions

  • good β€” checks if no error has occurred i.e. I/O operations are available (public member function of std::basic_ios<CharT,Traits>)
  • eof β€” checks if end-of-file has been reached (public member function of std::basic_ios<CharT,Traits>)
  • fail β€” checks if an error has occurred (public member function of std::basic_ios<CharT,Traits>)
  • bad β€” checks if a non-recoverable error has occurred (public member function of std::basic_ios<CharT,Traits>)
  • operator! β€” checks if an error has occurred (synonym of fail()) (public member function of std::basic_ios<CharT,Traits>)
  • operator bool β€” checks if no error has occurred (synonym of !fail()) (public member function of std::basic_ios<CharT,Traits>)
  • rdstate β€” returns state flags (public member function of std::basic_ios<CharT,Traits>)
  • setstate β€” sets state flags (public member function of std::basic_ios<CharT,Traits>)
  • clear β€” modifies state flags (public member function of std::basic_ios<CharT,Traits>)

πŸ’… Formatting

  • copyfmt β€” copies formatting information (public member function of std::basic_ios<CharT,Traits>)
  • fill β€” manages the fill character (public member function of std::basic_ios<CharT,Traits>)

🌈 Miscellaneous

  • exceptions β€” manages exception mask (public member function of std::basic_ios<CharT,Traits>)
  • imbue β€” sets the locale (public member function of std::basic_ios<CharT,Traits>)
  • rdbuf β€” manages associated stream buffer (public member function of std::basic_ios<CharT,Traits>)
  • tie β€” manages tied stream (public member function of std::basic_ios<CharT,Traits>)
  • narrow β€” narrows characters (public member function of std::basic_ios<CharT,Traits>)
  • widen β€” widens characters (public member function of std::basic_ios<CharT,Traits>)

πŸ’œ Inherited from std::ios_base πŸ’œ

The OG base class. Everything trickles down from here, bestie. 🫢

πŸŽ€ Member functions πŸŽ€

✨ Formatting

  • flags β€” manages format flags (public member function of std::ios_base)
  • setf β€” sets specific format flag (public member function of std::ios_base)
  • unsetf β€” clears specific format flag (public member function of std::ios_base)
  • precision β€” manages decimal precision of floating point operations (public member function of std::ios_base)
  • width β€” manages field width (public member function of std::ios_base)

🌍 Locales

  • imbue β€” sets locale (public member function of std::ios_base)
  • getloc β€” returns current locale (public member function of std::ios_base)

πŸ—‚οΈ Internal extensible array

  • xalloc β€” returns a program-wide unique integer that is safe to use as index to pword() and iword(), and it's marked [static] (public static member function of std::ios_base)
  • iword β€” resizes the private storage if necessary and access to the long element at the given index (public member function of std::ios_base)
  • pword β€” resizes the private storage if necessary and access to the void* element at the given index (public member function of std::ios_base)

πŸ¦‹ Miscellaneous

  • register_callback β€” registers event callback function (public member function of std::ios_base)
  • sync_with_stdio β€” sets whether C++ and C I/O libraries are interoperable, and it's marked [static] (public static member function of std::ios_base)

πŸ‘‘ Member classes

  • failure β€” stream exception (public member class of std::ios_base)
  • Init β€” initializes standard stream objects (public member class of std::ios_base)

✨ Member types and constants ✨

Okay bestie, this is the type-and-constant cheat sheet πŸ’… β€” every single name stays exactly as written, no cap. Let’s go.

🌸 stream open mode type

openmode β€” (typedef)

The following constants are also defined:

  • app β€” seek to the end of stream before each write
  • binary β€” open in binary mode
  • in β€” open for reading
  • out β€” open for writing
  • trunc β€” discard the contents of the stream when opening
  • ate β€” seek to the end of stream immediately after open
  • noreplace (C++23) β€” open in exclusive mode

πŸŽ€ formatting flags type

fmtflags β€” (typedef)

The following constants are also defined:

  • dec β€” use decimal base for integer I/O: see std::dec
  • oct β€” use octal base for integer I/O: see std::oct
  • hex β€” use hexadecimal base for integer I/O: see std::hex
  • basefield β€” dec | oct | hex. Useful for masking operations
  • left β€” left adjustment (adds fill characters to the right): see std::left
  • right β€” right adjustment (adds fill characters to the left): see std::right
  • internal β€” internal adjustment (adds fill characters to the internal designated point): see std::internal
  • adjustfield β€” left | right | internal. Useful for masking operations
  • scientific β€” generate floating point types using scientific notation, or hex notation if combined with fixed: see std::scientific
  • fixed β€” generate floating point types using fixed notation, or hex notation if combined with scientific: see std::fixed
  • floatfield β€” scientific | fixed. Useful for masking operations
  • boolalpha β€” insert and extract bool type in alphanumeric format: see std::boolalpha
  • showbase β€” generate a prefix indicating the numeric base for integer output, require the currency indicator in monetary I/O: see std::showbase
  • showpoint β€” generate a decimal-point character unconditionally for floating-point number output: see std::showpoint
  • showpos β€” generate a + character for non-negative numeric output: see std::showpos
  • skipws β€” skip leading whitespace before certain input operations: see std::skipws
  • unitbuf β€” flush the output after each output operation: see std::unitbuf
  • uppercase β€” replace certain lowercase letters with their uppercase equivalents in certain output operations: see std::uppercase

πŸ’œ state of the stream type

iostate β€” (typedef)

The following constants are also defined:

  • goodbit β€” no error
  • badbit β€” irrecoverable stream error
  • failbit β€” input/output operation failed (formatting or extraction error)
  • eofbit β€” associated input sequence has reached end-of-file

πŸ¦‹ seeking direction type

seekdir β€” (typedef)

The following constants are also defined:

  • beg β€” the beginning of a stream
  • end β€” the ending of a stream
  • cur β€” the current position of stream position indicator

🌈 event things

  • event β€” (enum) β€” specifies event type
  • event_callback β€” (typedef) β€” callback function type

That’s the whole list, bestie β€” every type, every constant, every little (typedef) and (enum) accounted for πŸ’–. This page came from http://cppreference.com, dated 2024.06.10, and it’s the std::ostrstream(3) page πŸŽ€.

πŸ“¦ where this page comes from