RFR: 8255082: HotSpot Style Guide should permit noexcept [v3]
Kim Barrett
kbarrett at openjdk.org
Fri Jun 20 17:23:28 UTC 2025
On Fri, 20 Jun 2025 16:44:16 GMT, Vladimir Kozlov <kvn at openjdk.org> wrote:
>> The standard allows `noexcept` to be used in two different contexts. It can be
>> used in a function's exception specification. It can instead be used in an
>> ordinary expression. In either case it takes one argument expression, but what
>> form that can take and how it is interpreted is quite different, depending on
>> which of those contexts applies.
>>
>> Used in an exception specification, the argument must be a constant
>> expression. So one can write a conditional exception specification, indicating
>> that a function is non-throwing if the argument is true, and potentially
>> throwing if false. One use-case for this is a generic container, where some
>> operations may only be noexept if certain operations of the element type are
>> noexcept. In this context, `noexcept` (without arguments) is equivalent to
>> `noexcept(true)`, and also to the deprecated `throw()` (with no arguments). At
>> this time I think we don't ever need arguments, and the no-arg abbreviation is
>> sufficient. That might change if we move toward using standard library
>> facilities (particularly containers), or make our own more like those in the
>> standard.
>>
>> Used as an expression, the argument is an unevaluated expression (similar to
>> some uses of sizeof and alignof). The result of the `noexcept` expression is a
>> bool constant, and is true if the expression never throws (based on examining
>> exception specifications of functions called in the expression), false
>> otherwise. There isn't an abbreviated expression form without arguments, as
>> that doesn't make sense. At this time I think we don't need `noexcept`
>> expressions, and might never need them.
>>
>> (Note that the argument expression for a `noexcept` exception specification
>> can include `noexcept` expressions. But since we're not currently allowing
>> either, that doesn't matter.)
>
> Got it. No `noexcept` in regular expressions. No `noexcept(foo())`.
>
> But we can use `foo() noexcept(true);` which is equivalent to ``foo() noexcept;`
>
> Do we have case for `noexcept(false);` in VM?
The guidance is to use `foo() noexcept` rather than `foo() noexcept(true)`.
The guidance is to not use an exception specification of `noexcept(false)`. That's equivalent
to not having an exception specification. So reduce clutter and leave it off. (I could imagine a
different style guide for a different, exception-using project, that said all functions should have
explicit exception specifications. I've never seen such. Beyond that, I don't think there's a
use case for `noexcept(false)` at all, VM or not.)
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/25574#discussion_r2159412800
More information about the hotspot-dev
mailing list