The following code does not compile in C++20
#include <iostream>
#include <cstddef>
int main(){
std::byte b {65};
std::cout<<"byte: "<<b<<'\n';// Missing overload
}
When std::byte
was added in C++17, why was there no corresponding operator<<
overloading for printing it? I can maybe understand the choice of not printing containers, but why not std::byte
? It tries to act as primitive type and we even have overloads for std::string
, the recent std::string_view
, and perhaps the most related std::complex
, and std::bitset
itself can be printed.
There are also std::hex
and similar modifiers, so printing 0-255 by default should not be an issue.
Was this just oversight? What about operator>>
, std::bitset
has it and it is not trivial at all.
EDIT: Found out even std::bitset
can be printed.
From the paper on
std::byte
(P0298R3): (emphasis mine)(emphasis mine)
As such, it is not required to be implicitly convertible/interpreted to be either a
char
or any integral type whatsoever and hence cannot be printed usingstd::cout
unless explicitly cast to the required type.Furthermore, see How to use new std::byte type in places where old-style unsigned char is needed?.