How can I properly overload the

I am just telling you about one other possibility: I like using friend definitions for that: namespace Math { class Matrix { public: […] friend std::ostream& operator<< (std::ostream& stream, const Matrix& matrix) { […] } }; } The function will be automatically targeted into the surrounding namespace Math (even though its definition appears within the … Read more

Why is operator

The set of inserters for std::basic_ostream includes partial specializations for inserting char, signed char, unsigned char and such into basic_ostream<char, …> streams. Note that these specializations are made available for basic_ostream<char, …> streams only, not for basic_ostream<wchar_t, …> streams or streams based on any other character type. If you move these freestanding templates into the … Read more

Why is istream/ostream slow

Actually, IOStreams don’t have to be slow! It is a matter of implementing them in a reasonable way to make them fast, though. Most standard C++ library don’t seem to pay too much attention to implement IOStreams. A long time ago when my CXXRT was still maintained it was about as fast as stdio – … Read more

Floating point format for std::ostream

std::cout << std::fixed << std::setw(11) << std::setprecision(6) << my_double; You need to add #include <iomanip> You need stream manipulators You may “fill” the empty places with whatever char you want. Like this: std::cout << std::fixed << std::setw(11) << std::setprecision(6) << std::setfill(‘0’) << my_double;

How to use C++ std::ostream with printf-like formatting?

In C++20 you can use std::format for safe printf-like formatting: std::cout << std::format(“The answer is {}.\n”, 42); In addition to that the {fmt} library, std::format is based on, provides the print function that combines formatting and output: fmt::print(“The answer is {}.\n”, 42); Disclaimer: I’m the author of {fmt} and C++20 std::format.