No, it isn't possible. The aliasing exception is specific only to char
, unsigned char
and std::byte
. You can't define a type in standard C++ to also gain their "superpower".
There may be compiler-specific attributes that can give a type declared with
enum class byte : unsigned char {};
equivalent aliasing exceptions, although aliasing isn't the only exceptional behavior either.
Technically std::byte
and unsigned char
are also specific exceptions in the core language that are able to provide storage for other objects and the only types that can cause implicit creation of objects when the lifetime of an array of their type starts. Other types can't do either. However, on the usual C++ implementations that part is probably not really all that relevant in practice.
std::byte
and unsigned char
(and potentially char
) are also the only types for which it is sometimes allowed to operate on indeterminate values in some very specific circumstances. But again, on usual C++ implementations that probably isn't going to be a practical problem.
There is also an exception for array-new expressions that guarantees specifically only for char
, unsigned char
and std::byte
that there is no offset between the beginning of the allocation and the beginning of the array. Again though, in practice implementations will probably extend this exception to all trivial types.
unsigned char
gets the free-pass. – Aldasbyte
type withstd::uint8_t
– Clingfishbyte
name because I think it makes the API easier to read. part of my problem is thatstd::byte
is explicitly mentioned in the C++17 standard in places wherechar
andunsigned char
are mentioned in relation to aliasing, similar to how it is done here: en.cppreference.com/w/cpp/language/… – Offshorestd
namespace is not allowed to be extended apart from specialising templates from. – Suellenenum class
follows the same aliasing rules as its underlying type or whether compiler magic is used forstd::byte
. – Higginbothamchar
: It's bit size can vary across architectures… I personally rather stick touint8_t
– it will not exist on systems without an appropriate type to be mapped to, so you are hinted to necessary special handling on such platforms immediately. – Suellen