RFR: 8377895: Create sizeof_auto to reduce narrowing conversions [v2]
Leo Korinth
lkorinth at openjdk.org
Tue Feb 24 13:02:16 UTC 2026
On Mon, 23 Feb 2026 18:42:01 GMT, Albert Mingkun Yang <ayang at openjdk.org> wrote:
>> I think your solution is the very best solution. I will use it.
>
> 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`)
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/29716#discussion_r2846987196
More information about the hotspot-dev
mailing list