std::basic_ios::copyfmt(3)

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

πŸ’– std::basic_ios::copyfmt πŸ’–

okay so welcome to the C++ Standard Libary manual page for std::basic_ios::copyfmt, bestie. we're copying stream state today and it's gonna be iconic. 🫢


✨ NAME ✨

  • std::basic_ios::copyfmt β€” std::basic_ios::copyfmt (yes, the name repeats; it's giving symmetry, no notes πŸ’…).

🌸 Synopsis 🌸

basic_ios& copyfmt( const basic_ios& other );

if other refers to the same object as *this, it has no effects. otherwise, it copies the state of the stream other into *this. and the sequence is exactly this, bestie:

  1. Calls every callback registered by register_callback() passing erase_event as parameter.
  2. Copies all member objects from other to *this except for rdstate(), the exception mask, and rdbuf(). In particular, it makes copies of the locale, the formatting flags, the contents of the arrays std::ios_base::iword and std::ios_base::pword (but not the iword and pword pointers themselves), the callbacks, and the tied stream.
  3. Calls every callback registered by register_callback() passing copyfmt_event as parameter.
  4. Copies the exception mask from other to *this as if by calling exceptions(other.exceptions()).

πŸŽ€ Parameters πŸŽ€

  • other β€” another stream to use as source.

πŸ‘‘ Return value πŸ‘‘

  • *this β€” that's the return value, periodt.

πŸ¦‹ Notes πŸ¦‹

the second pass through the callbacks may be used to deep-copy the user-defined objects pointed to by the pointers in std::ios_base::pword. that's the whole vibe.

also: copyfmt() may be used to save and restore the state of a stream. Boost provides a more fine-grained I/O state savers library for the same purpose.

🌈 Example 🌈

Makes the std::ofstream object "out" behave exactly like std::cout, including formatting, tie() to std::cin, etc.

Run this code, bestie:

#include <bitset>
#include <climits>
#include <fstream>
#include <iostream>

int main()
{
    std::ofstream out;

    out.copyfmt(std::cout); // copy everything except rdstate and rdbuf
    out.clear(std::cout.rdstate()); // copy rdstate
    out.basic_ios<char>::rdbuf(std::cout.rdbuf()); // share the buffer

    out << "Hello, world\n";

    auto bin = [](std::ios_base::fmtflags f)
    {
        return std::bitset<sizeof(std::ios_base::fmtflags) * CHAR_BIT>
            { static_cast<unsigned long long>(f) };
    };
    std::ofstream out2;
    std::cout << "1) out2.flags(): " << bin(out2.flags()) << '\n';
    std::cout << "2) cout.flags(): " << bin(std::cout.flags()) << '\n';
    std::cout.setf(std::ios::hex | std::ios::fixed |
       std::ios::boolalpha);
    std::cout << "3) cout.flags(): " << bin(std::cout.flags()) << '\n';
    out2.copyfmt(std::cout); // copy everything except rdstate and
       rdbuf
    std::cout << "4) out2.flags(): " << bin(out2.flags()) << '\n';
}

πŸ“ Possible output πŸ“

Hello, world
1) out2.flags(): 00000000000000000001000000000010
2) cout.flags(): 00000000000000000001000000000010
3) cout.flags(): 00000000000000000001000000001111
4) out2.flags(): 00000000000000000001000000001111

🩹 Defect reports 🩹

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. no cap, they changed the past:

  • LWG 256 β€” applied to C++98. Behavior as published: step 3 called the registered callbacks with the event type copy_event, which is not defined. Correct behavior: corrected to copyfmt_event.
  • LWG 292 β€” applied to C++98. Behavior as published: if other refers to the same object as *this, the member objects were still copied and the registered callbacks were still called. Correct behavior: do nothing in this case.

source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::basic_ios::copyfmt(3) β€” stay hydrated and may your streams always be tied, bestie πŸ’–πŸŽ€

πŸ“¦ where this page comes from