What is the C++ iostream endl fiasco?

(I assume) He just means that many, especially new, C++ programmers use std::endl blindly instead of ‘\n’ for newline, flushing unnecessarily frequently and potentially making the performance of their program abysmal. I.e., most people are taught that std::endl is the canonical way to insert a newline into a stream even though it is very rarely … Read more

How to print __int128 in g++?

If you don’t need any of the fancy formatting options, writing your own << operator is trivial. Formally, I suspect that writing one for __int128_t would be considered undefined behavior, but practically, I think it would work, up until the library starts providing actual support for it (at which point, you’d retire your conversion operator). … Read more

Standard no-op output stream

You need a custom streambuf. class NullBuffer : public std::streambuf { public: int overflow(int c) { return c; } }; You can then use this buffer in any ostream class NullBuffer null_buffer; std::ostream null_stream(&null_buffer); null_stream << “Nothing will be printed”; streambuf::overflow is the function called when the buffer has to output data to the actual … Read more

Why do C++ streams use char instead of unsigned char?

Possibly I’ve misunderstood the question, but conversion from unsigned char to char isn’t unspecified, it’s implementation-dependent (4.7-3 in the C++ standard). The type of a 1-byte character in C++ is “char”, not “unsigned char”. This gives implementations a bit more freedom to do the best thing on the platform (for example, the standards body may … Read more

Why can’t std::ostream be moved?

Originally they were movable. This turned out to be a design flaw on my part, and discovered by Alberto Ganesh Barbati: http://cplusplus.github.io/LWG/lwg-defects.html#911 The issue shows a few examples where ostream gets moved and/or swapped, and the results are surprising, instead of expected. I was convinced that these types should not be publicly movable nor swappable … Read more

Obtain a std::ostream either from std::cout or std::ofstream(file)

std::streambuf * buf; std::ofstream of; if(!condition) { of.open(“file.txt”); buf = of.rdbuf(); } else { buf = std::cout.rdbuf(); } std::ostream out(buf); That associates the underlying streambuf of either cout or the output file stream to out. After that you can write to “out” and it will end up in the right destination. If you just want … Read more

ifstream: check if opened successfully

operator! is overloaded for std::ifstream, so you can do this. In my opinion, though, this is a horrible abuse of operator overloading (by the standards committee). It’s much more explicit what you’re checking if you just do if (stream.fail()).

Are int8_t and uint8_t intended to be char types?

From § 18.4.1 [cstdint.syn] of the C++0x FDIS (N3290), int8_t is an optional typedef that is specified as follows: namespace std { typedef signed integer type int8_t; // optional //… } // namespace std § 3.9.1 [basic.fundamental] states: There are five standard signed integer types: “signed char”, “short int”, “int”, “long int”, and “long long … Read more

Why std::cout instead of simply cout?

It seems possible your class may have been using pre-standard C++. An easy way to tell, is to look at your old programs and check, do you see: #include <iostream.h> or #include <iostream> The former is pre-standard, and you’ll be able to just say cout as opposed to std::cout without anything additional. You can get … Read more

How to get IOStream to perform better?

Here is what I have gathered so far: Buffering: If by default the buffer is very small, increasing the buffer size can definitely improve the performance: it reduces the number of HDD hits it reduces the number of system calls Buffer can be set by accessing the underlying streambuf implementation. char Buffer[N]; std::ifstream file(“file.txt”); file.rdbuf()->pubsetbuf(Buffer, … Read more