""_hex is a compile-time user-defined literal returning a std::array<std::byte>
, equivalent to ParseHex().
Variants provided:
- ""_hex_v: Returns
std::vector<std::byte>
, useful for heap allocation or variable-length serialization.
- ""_hex_u8: Returns
std::array<uint8_t>
, for cases where std::byte
is incompatible.
- ""_hex_v_u8: Returns
std::vector<uint8_t>
, combining heap allocation with uint8_t
.
- Warning
- It could be necessary to use vector instead of array variants when serializing, or vice versa, because vectors are assumed to be variable- length and serialized with a size prefix, while arrays are considered fixed length and serialized with no prefix.
-
It may be preferable to use vector variants to save stack space when declaring local variables if hex strings are large. Alternatively variables could be declared constexpr to avoid using stack space.
-
Avoid
uint8_t
variants when not necessary, as the codebase migrates to use std::byte
instead of unsigned char
and uint8_t
.
- Note
- One reason ""_hex uses
std::array
instead of std::vector
like ParseHex() does is because heap-based containers cannot cross the compile- time/runtime barrier.