RFR: 8377895: Create sizeof_auto to reduce narrowing conversions [v2]

Leo Korinth lkorinth at openjdk.org
Tue Feb 24 13:12:22 UTC 2026


On Tue, 24 Feb 2026 12:59:50 GMT, Leo Korinth <lkorinth at openjdk.org> wrote:

>> This is probably subjective. I find this nested `conditional_t` rather hard to parse/follow. I wonder what you think of `if constexpr`.
>> 
>> 
>> template<size_t N>
>> constexpr auto sizeof_auto_impl() {
>>     if constexpr (N <= 0xFF)       return uint8_t(N);
>>     if constexpr (N <= 0xFFFF)     return uint16_t(N);
>>     if constexpr (N <= 0xFFFFFFFF) return uint32_t(N);
>>     return uint64_t(N);
>> }
>
> I much prefer that version (I tried something similar), unfortunately it does not work. I am a bit unsure exactly why, but I think your version will deduce different return types for auto, where my version will deduce the same type alias (to different types). That is just a guess though.
> 
> If you can fix it, I would prefer your version (with `std::numeric_limits<uint8_t>::max()` instead of `0xFF`)

I got help from @xmas92, and I think I have a working version. I will show it soon after some testing.

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

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


More information about the hotspot-dev mailing list