RFR: 8314488: Compiling the JDK with C++17
Andrew Haley
aph at openjdk.org
Mon Aug 18 08:50:16 UTC 2025
On Mon, 18 Aug 2025 07:17:09 GMT, Johan Sjölen <jsjolen at openjdk.org> wrote:
>> doc/cpp17-features.md line 128:
>>
>>> 126: [n4147](http://wg21.link/n4147)
>>> 127: [n4424](http://wg21.link/n4424)
>>> 128: [p0386r2](http://wg21.link/p0386r2)
>>
>> After looking at inline variables more carefully as part of writing it up for
>> the style guide, I'm inclined to move inline variables to the undecided or
>> forbidden category. They seem to be a solution to a problem I think we don't
>> really have.
>>
>> Basically, they let you provide the full definition for a non-local variable
>> in a header file, rather than declaring it in the header and defining it in
>> an associated .cpp file. The motivating use-case is a component or library
>> that is header only, to simplify client use by avoiding build issues. But
>> that's just not a thing we care about for HotSpot code.
>>
>> So what WE get from an inline variable is having a complete declaration and
>> definition in one place, rather than separating the declaration from the
>> definition (so probably saving a little bit of code), at the cost of putting
>> the initialization expression in the header (which might require pulling other
>> stuff into the header), and maybe making the compiler work a little harder.
>>
>> The benefit of that seems minimal at best.
>>
>> The one part of it that seems slightly useful is that a constexpr variable is
>> implicitly inline, so doesn't need a definition without an initializer in a
>> .cpp file if the variable is ODR-used. Indeed, that's now treated as a
>> duplicate defintion and deprecated usage. But we haven't had any warnings, so
>> guessing we don't actually have any of those. We can take advantage of this
>> for future usage.
>>
>> Any other opinions?
>
> It's a bit of ceremony to do the header + source file edits for static variables, but maybe it's one worth living with to not have to explain to people what an inline variable is. I think that they'll be accepting of `static constexpr` variables being defined inline, as there's no inline keyword involved and constexpr is already special anyway.
> Any other opinions?
The benefit is minimal, but the cost is pretty low, and people will get used to it soon enough. Also, you'd have to explain to HotSpot newcomers why this feature is bad, but it isn't really bad. If it'd always been like that, no one would be at all bothered by it.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/25992#discussion_r2281720573
More information about the hotspot-dev
mailing list