Remember that in C++, the canonical way to represent a byte is char
, which is an integral type. 1
The C++ standard provides the Input/Output Library, which provides basic input and output streams. These basic streams are templates, and the 'character' type that they work on is chosen with a template parameter. Typically, these are used in the char
and "wide character" wchar_t
flavours. Other character types exist, like char8_t
and char32_t
, but their usage is a bit uncommon.
For example, file input and output in C++ can be done with a basic_fstream
object, which takes as template parameter some type CharT
. For convenience, we have two aliases: fstream
is shorthand for basic_fstream<char>
, and wfstream
is shorthand for basic_fstream<wchar_t>
. This pattern is also seen across other streams, like stringstreams and the standard input and output streams.
At a glance, it might seem like the "wide character" wchar_t
would be great for working with Unicode strings. Don't be fooled. Unicode support in C++ is seriously lacking.
1. Admittedly, there is now a standard std::byte
, but it's usage is fairly limited. It's neither an integral type nor a character type.
Note: Don't mix cout
and wcout
in the same program. Here's why.