Home > Software design >  How do I make the `std::ios_base::copyfmt_event` happen in my tests?
How do I make the `std::ios_base::copyfmt_event` happen in my tests?

Time:07-05

I wrote a set of functions to print out an address in my libaddr library (see addr.h header).

I am easily able to test the erase_event by changing one of my format flags:

std::cout << addr::setaddrsep("\n") << addresses;

I do not care about the imbue_event (the locale has no effect on IP addresses).

What I'm wondering is how to generate a copyfmt_event. When does that happen? Since it is expected to duplicate my structure, I need to make sure that part works as expected (no double delete, no leaks, proper copy).

Reference: https://en.cppreference.com/w/cpp/io/ios_base/event

CodePudding user response:

From https://en.cppreference.com/w/cpp/io/basic_ios/copyfmt :

#include <iostream>
#include <fstream>
 
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";
}
  • Related