std::basic_ios::copyfmt(3)
π 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:
- Calls every callback registered by
register_callback()passingerase_eventas parameter. - Copies all member objects from
otherto*thisexcept forrdstate(), the exception mask, andrdbuf(). In particular, it makes copies of the locale, the formatting flags, the contents of the arraysstd::ios_base::iwordandstd::ios_base::pword(but not theiwordandpwordpointers themselves), the callbacks, and the tied stream. - Calls every callback registered by
register_callback()passingcopyfmt_eventas parameter. - Copies the exception mask from
otherto*thisas if by callingexceptions(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 tocopyfmt_event. - LWG 292 β applied to C++98. Behavior as published: if
otherrefers 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms