Why aren't iostreams copied?

to create a local copy of the iostream object using rdbufand copyfmt. This allows you to change the formatting in local mode:

std::ostream & operator << ( std::ostream & os, foo const & smth ) {
    cloned_ostream cs( os );
    cs << std::hex << smth.num;
    // os is not switched to hexadecimal, which would be a confusing side-effect
    return os;
}

Why don't thread classes provide copy constructors for this?

Have the appropriate C ++ best practices changed since they were designed as not copyable?

+4
source share
1 answer

- . , , . iostreams, ++ 11 , .

: . ++ 98, rdbuf, rdstate copyfmt .

++ 11, protected, ( move), , . iostream .

, copyfmt, .

rdbuf , std::fstream, , .

std::ifstream f( path + filename ); // Owns, or even "is," a file.
std::istream i = f; // Observes an externally-managed file.

std::istream i2 = i; // OK, copy a shallow reference.
std::ifstream f2 = f; // Error, ifstream is more than a shallow reference.

std::istream i3 = std::move( f ); // Error? Would retain a reference to an rvalue.
std::ifstream f3 = std::move( f ); // OK: full copy including the file buffer.

, .

+5

All Articles