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

Leo Korinth lkorinth at openjdk.org
Wed Feb 18 09:44:36 UTC 2026


On Tue, 17 Feb 2026 17:22:21 GMT, Kim Barrett <kbarrett at openjdk.org> wrote:

>> Oh, but we don't need to worry about variadic macros here.  `sizeof_auto<T>()` can't
>> be a function-like macro invocation.  So something like this (not tested) should work:
>> 
>> #define sizeof_auto(...) sizeof_auto<decltype(__VA_ARGS__)>()
>> 
>> It's a variadic macro so that we don't need to parenthesize an expression containing
>> unparenthesized commas (like a template argument list).
>> 
>> A different syntactic option would be a macro for both cases, e.g. `sizeof_auto(X)`
>> where `X` is either a type or an expression, just like `sizeof`.  I remember a trick for
>> distinguishing between those, though would need to do some dredging to recall the
>> details.  But while it would be "fun" it might be considered a bit more effort than the
>> nice and consistent syntax is worth.  I might give it a try, just to see how hard it really is.
>
> I remembered enough of the trick to think it probably doesn't work here.  However, there's a variant, using C++17 features, that I think probably would work.  But I didn't explore in that direction because I realized there's a *much* simpler approach; just use `sizeof` to deal with that dispatch.  Here's my (untested, hopefully no syntax errors) solution:
> 
> template<size_t N>
> constexpr auto sizeof_auto_impl() {
>   using unsigned_auto =
>     std::conditional_t<N <= std::numeric_limits<uint8_t>::max(), uint8_t,
>       std::conditional_t<N <= std::numeric_limits<uint16_t>::max(), uint16_t,
>         std::conditional_t<N <= std::numeric_limits<uint32_t>::max(), uint32_t, uint64_t>>>;
>   return static_cast<unsigned_auto>(N);
> }
> #define sizeof_auto(...) sizeof_auto_impl<sizeof(__VA_ARGS__)>()

I think your solution is the very best solution. I will use it.

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

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


More information about the hotspot-dev mailing list