Questions tagged [std-byte]
27 questions
117
votes
9 answers
Is there 'byte' data type in C++?
If it exists, is there header file to include?
This code results in a compilation error:
int main() {
byte b = 2; // error
}

user2972135
- 1,361
- 2
- 8
- 10
65
votes
4 answers
How to use new std::byte type in places where old-style unsigned char is needed?
std::byte is a new type in C++17 which is made as enum class byte : unsigned char. This makes impossible to use it without appropriate conversion. So, I have made an alias for the vector of such type to represent a byte array:
using Bytes =…

VP.
- 15,509
- 17
- 91
- 161
35
votes
1 answer
What is the purpose of std::byte?
Now that c++17 has std::byte, I was looking for a way to convert code that reads files to char into code that reads files into byte. A file contains bytes, not a bunch of integers.
Then I read this question and this other question where people…
user8370684
31
votes
2 answers
Why is `std::byte` an enum class instead of a class?
std::byte is an abstraction that is supposed to provide a type safe(r) access to regions of memory in C++, starting with the new standard 17. However, it's declared this way according to http://en.cppreference.com/w/cpp/types/byte:
enum class byte :…

Nir Friedman
- 17,108
- 2
- 44
- 72
30
votes
2 answers
Is std::byte well defined?
C++17 introduces the std::byte type. A library type that can (supposedly) be used to access raw memory, but stands separate from the character types and represents a mere lump of bits.
So far so good. But the definition has me slightly worried. As…

StoryTeller - Unslander Monica
- 165,132
- 21
- 377
- 458
23
votes
2 answers
Why is there no overload for printing `std::byte`?
The following code does not compile in C++20
#include
#include
int main(){
std::byte b {65};
std::cout<<"byte: "<

Quimby
- 17,735
- 4
- 35
- 55
12
votes
1 answer
Only bitwise operations for std::byte in C++17?
In CPP Reference it is stated that:
std::byte is a distinct type that implements the concept of byte as specified in the C++ language definition.
Like char and unsigned char, it can be used to access raw memory occupied by other objects (object…

wimalopaan
- 4,838
- 1
- 21
- 39
11
votes
2 answers
Correct way to initialize a container of std::byte
What is the correct way of initializing a container with predetermined std::byte values?
std::array arr{0x36, 0xd0} for array results in
Enum std::byte has not constant to represent the integer value of X
and compiler errors. Vector…

mmatous
- 482
- 6
- 16
10
votes
3 answers
Read file into std::vector
I'm trying to read a file in binary format into a std::vector
std::ifstream fStream(fName, std::ios::binary);
std::vector file_content((std::istreambuf_iterator(fStream)),
…

Théo Champion
- 1,701
- 1
- 21
- 46
8
votes
2 answers
Testing the availability of std::byte
I'd like to use C++17's std::byte type if it's available, and fall back to using unsigned char if not, i.e. something along the lines of
#include
namespace my {
#if SOMETHING
using byte = std::byte;
#else
using byte = unsigned…

Tristan Brindle
- 16,281
- 4
- 39
- 82
6
votes
0 answers
std::byte overhead from integer promotion
Consider an unsigned char v that goes through a series of bit-wise operations with the result stored back to v. Under the hood, it is integer promoted once, undergoes a series of operations, and the result is truncated and stored back to v.
With…

Lingxi
- 14,579
- 2
- 37
- 93
6
votes
1 answer
std::byte on odd platforms
Reading Herb Sutter's blog post about the most recent C++ standard meeting, it noticed that std::byte was added to C++17. As an initial reading, I have some concerns since it uses unsigned char so that it can avoid complications with strict…

Graznarak
- 3,626
- 4
- 28
- 47
5
votes
2 answers
Is it possible to backport std::byte to C++14
std::byte is defined in C++17 as:
enum class byte : unsigned char {};
I'm currently stuck at using C++14, and I wonder if I add the same definition in C++14 (in some non-std namespace, along with the operator overloads etc.), will this new type get…

Erik Man
- 824
- 4
- 17
5
votes
1 answer
C++17 std::byte produces less optimized code with the standard algorithms in GCC
I really like std::byte as a distinct type that implements the concept of byte as specified in the C++ language definition. What I don't like is the fact that modern C++ compilers will produce less optimized code using the standard algorithms.
Here…
user21009021
3
votes
1 answer
How to define a static array of std::byte in C++?
I've got this long list of bytes (as in, they don't represent chars or int).
At the moment, I can just do the following:
static const char myArray[] = {0xb8, 0xfe, 0x6c, 0x39, 0x23, ...}
What I would like is to replace char with std::byte, but the…

Andi Abrudan
- 104
- 4