Hello All,
The current std::byte has some deep issues in that it is too opaque to the point it is flawed by design.
(we know it is in reality a scoped enum).
Especially this comes up in generic code where it is cumbersome to add specializations because it is not a fundamental type.
A "better" way would be a real fundamental type or struct that mimicks it...
Overall it should at least behave like a fundamental uint8_t/unsigned char in most respects
and be easy to convert to/from...
except ofc. it shouldn't be an arithmetic type and it shouldn't be treated like a textual "char" as we know.
See below for a quick draft up...
Kind regards
Rune Lund Olesen
(writing c++ for ~20 years)
Senior Engineer
Gatehouse SatCom
Denmark (sorry for possible quickly written english)
namespace std
{
struct byte2 {
byte2(){};
byte2(uint8_t value) : value{value} {
}
explicit operator uint8_t() const {
return value;
}
// more usefull functions, overloads, whatever
uint8_t value{};
};
template<>
struct is_fundamental<std::byte2> : true_type {
};
template<>
struct is_unsigned<std::byte2> : true_type {
};
//more type traits
}
static_assert(std::is_fundamental_v<std::byte2>);
static_assert(std::is_unsigned_v<std::byte2>);
void f() {
std::vector<std::byte2> mybytes{};
std::vector<uint8_t> mybytes_uint {mybytes.begin(), mybytes.end()};
std::byte2 b;
uint8_t f;
//f = f + b; //will give compiler error
}