RFR: 8274400: HotSpot Style Guide should permit use of alignof [v4]
Kim Barrett
kbarrett at openjdk.org
Sun Jan 1 02:18:49 UTC 2023
On Sat, 31 Dec 2022 17:18:52 GMT, Julian Waters <jwaters at openjdk.org> wrote:
>> I'm not sure if it's a good idea to entirely forbid `std::alignment_of<>`? While `alignof` should be used over it most of the time, I'm somewhat certain it and it's other counterpart from C++17 (may) have certain differences from `alignof` in newer versions of the language (especially when used with templates and in metaprogramming contexts), which is why I chose to avoid having to take those issues into account for now by simply saying the latter should always be preferred over the former (Similar to how nullptr vs NULL is discussed in the Style Guide). Maybe I could word it better though
>
> Something like this perhaps?
>
> - Prefer `alignof` ([n2341](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2341.pdf)) to `std::alignment_of<>`. The template has been superseded by the newer operator.
> I'm not sure if it's a good idea to entirely forbid `std::alignment_of<>`?
I disagree.
> While `alignof` should be used over it most of the time, I'm somewhat certain
> it and it's other counterpart from C++17 (may) have certain differences from
> `alignof` in newer versions of the language ...
`std::alignment_of<T>::value` is defined as `alignof(T)` in every version of
the standard. The C++17 addition of `std::alignment_of_v<T>` is just
abbreviated syntactic sugar, and is still more verbose than using `alignof`. I
see no other changes between versions of the standard, and would be surprised
if any were to come in the future.
> ... (especially when used with templates and in metaprogramming contexts) ...
`std::alignment_of<>` could be used in advanced metaprogramming contexts
involving higher order metafunctions. We don't do that sort of thing much at
all in HotSpot. If someone finds a place where `std::alignment_of<>` might be
better, we can revisit its status.
> (Similar to how nullptr vs NULL is discussed in the Style Guide).
nullptr vs NULL is a very different situation. New code and substantial
changes should use nullptr. But there is a large body of existing code using
NULL. When making a small modification to such code one might choose to
continue using NULL to be consistent with surrounding code. And one might
choose to keep such a change separate from a NULL => nullptr change in that
surrounding code. There are no current uses of `std::alignment_of<>` to be
consistent with.
-------------
PR: https://git.openjdk.org/jdk/pull/11761
More information about the hotspot-dev
mailing list