If it exists, is there header file to include?
This code results in a compilation error:
int main() {
byte b = 2; // error
}
If it exists, is there header file to include?
This code results in a compilation error:
int main() {
byte b = 2; // error
}
No, there is no type called "byte
" in C++. What you want instead is unsigned char
(or, if you need exactly 8 bits, uint8_t
from <cstdint>
, since C++11). Note that char
is not necessarily an accurate alternative, as it means signed char
on some compilers and unsigned char
on others.
No there is no byte data type in C++. However you could always include the bitset header from the standard library and create a typedef for byte:
typedef bitset<8> BYTE;
NB: Given that WinDef.h defines BYTE for windows code, you may want to use something other than BYTE if your intending to target Windows.
Edit: In response to the suggestion that the answer is wrong. The answer is not wrong. The question was "Is there a 'byte' data type in C++?". The answer was and is: "No there is no byte data type in C++" as answered.
With regards to the suggested possible alternative for which it was asked why is the suggested alternative better?
According to my copy of the C++ standard, at the time:
"Objects declared as characters (char) shall be large enough to store any member of the implementations basic character set": 3.9.1.1
I read that to suggest that if a compiler implementation requires 16 bits to store a member of the basic character set then the size of a char would be 16 bits. That today's compilers tend to use 8 bits for a char is one thing, but as far as I can tell there is certainly no guarantee that it will be 8 bits.
On the other hand, "the class template bitset<N> describes an object that can store a sequence consisting of a fixed number of bits, N." : 20.5.1. In otherwords by specifying 8 as the template parameter I end up with an object that can store a sequence consisting of 8 bits.
Whether or not the alternative is better to char, in the context of the program being written, therefore depends, as far as I understand, although I may be wrong, upon your compiler and your requirements at the time. It was therefore upto the individual writing the code, as far as I'm concerned, to do determine whether the suggested alternative was appropriate for their requirements/wants/needs.
Using C++11
there is a nice version for a manually defined byte type:
enum class byte : std::uint8_t {};
That's at least what the GSL does.
Starting with C++17
(almost) this very version is defined in the standard as std::byte
(thanks Neil Macintosh for both).
No, but since C++11 there is [u]int8_t.
namespace std
{
// define std::byte
enum class byte : unsigned char {};
};
This if your C++ version does not have std::byte will define a byte type in namespace std. Normally you don't want to add things to std, but in this case it is a standard thing that is missing.
std::byte from the STL does much more operations.
in c++ char is 8 bit in length but the value differs depending on the use so if you need it to be exact value you have to use its prefixed versions such as
unsigned 0 - 255
signed (-127) - 127 (with a sign bit in front)
if you only use char it will auto select type of variable according to the value we insert and sometime it can be give unpredictable results.
there is also some other types for handling unicodes as chars
char16_t
char32_t
wchar_t