It is fairly common to have a literal integral value that needs to be of type size_t. This is usually accomplished by casting or explicit conversion of a literal integer. It would be simpler and more readable if we had literal syntax for size_t literals.
C++23 adds "zu" (in either order, and with either case for either letter) as a suffix for size_t, similar to the long-standing "u", "ul", "ull" and friends. But we don't have to wait for C++23 to get much the same thing. We can add "_zu" as a user-defined literal suffix. (The "_" is required to avoid reserved syntax and likely requiring the suppression of warnings.)
The HotSpot Style Guide says "User-defined literals should not be added casually, but only through a proposal to add a specific UDL." So this JBS issue and its (eventual) PR are proposing exactly that, implemented by adding something like the following in globalDefinitions.hpp:
{@code}
constexpr operator ""_zu(unsigned long long int x) {
// Probably only relevant for 32bit platforms.
assert(x <= std::numeric_limits<size_t>::max(), "invalid size_t literal");
return static_cast<size_t>(x);
}
{@code}
We don't need to support different orders and different cases (as C+23 does); just the one suffix seems sufficient.
We could also define "_z" for ssize_t, but that's a lot less commonly needed. (I did some searches and only found 1, and the cast isn't actually needed.)
C++23 adds "zu" (in either order, and with either case for either letter) as a suffix for size_t, similar to the long-standing "u", "ul", "ull" and friends. But we don't have to wait for C++23 to get much the same thing. We can add "_zu" as a user-defined literal suffix. (The "_" is required to avoid reserved syntax and likely requiring the suppression of warnings.)
The HotSpot Style Guide says "User-defined literals should not be added casually, but only through a proposal to add a specific UDL." So this JBS issue and its (eventual) PR are proposing exactly that, implemented by adding something like the following in globalDefinitions.hpp:
{@code}
constexpr operator ""_zu(unsigned long long int x) {
// Probably only relevant for 32bit platforms.
assert(x <= std::numeric_limits<size_t>::max(), "invalid size_t literal");
return static_cast<size_t>(x);
}
{@code}
We don't need to support different orders and different cases (as C+23 does); just the one suffix seems sufficient.
We could also define "_z" for ssize_t, but that's a lot less commonly needed. (I did some searches and only found 1, and the cast isn't actually needed.)