On Friday, March 14, 2025, Jan Schultke wrote:

typedef std::array< char unsigned, (128u / CHAR_BIT) + !!(128u % CHAR_BIT) > uuid_t;

I think UUIDs would be a good fit for the C++ standard library, but as a vocabulary class type, not just some alias for an array.

Keep in mind that there are different versions of UUIDs, and not every 128-bit pattern is a valid UUID. Version 4 in particular is the one populated with random numbers. If we did have 128-bit integers, this is how we could generate a random UUID:

std::uint128_t random_uuid_v4() {
    return std::experimental::randint<std::uint128_t>(0, -1)
         & 0xffff'ffff'ffff'003f'ff0f'ffff'ffff'ffff  // clear version and variant
         | 0x0000'0000'0000'0080'0040'0000'0000'0000; // set to version 4 and IETF variant
}


Notice how certain bits are constant.



I'm not denying that that's how things started out.

But nowadays, whenever I need a UUID for anything, I just generate a 128-Bit random number. I don't manually set the 6 bits to indicate that it's version 4. And I think that's what everyone else is doing now too.