RFR: 8377895: Create sizeof_auto, to reduce narrowing conversions [v7]

Andrew Haley aph at openjdk.org
Sun Feb 22 11:03:11 UTC 2026


On Thu, 19 Feb 2026 22:29:41 GMT, Leo Korinth <lkorinth at openjdk.org> wrote:

>> `sizeof_auto` is created so that we can reduce the amount of casting and thus make the code more type safe. The normal `sizeof` will return a `size_t` although the size of most stuff can be represented in a `uint8_t`. `sizeof_auto` will return the size in an as small unsigned type as is possible. The result is that expressions that uses `sizeof_auto` can convert to most integral types, and no explicit narrowing cast will be needed.
>> 
>> The change in `cpCache.hpp` is an example where we would get an implicit narrowing warning (if we had those enabled) using normal `sizeof`, but where `sizeof_auto` will not make the expression a `size_t` .
>
> Leo Korinth has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Line break comments, and add compiler error code on Windows to comment

src/hotspot/share/utilities/globalDefinitions.hpp line 179:

> 177:         std::conditional_t<N <= std::numeric_limits<uint32_t>::max(), uint32_t, uint64_t>>>;
> 178:   return static_cast<unsigned_auto>(N);
> 179: }

If I may butt in... This function has nothing to do with `sizeof`, so its name should not contain "sizeof". Its current name is misleading.
This function is of general use, so much so that it perhaps could even be part of the standard C++ language. `auto_size()` is not a bad choice of name for such a function.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/29716#discussion_r2837603001


More information about the hotspot-dev mailing list