JDK-8208089 : JEP 347: Enable C++14 Language Features
  • Type: JEP
  • Component: hotspot
  • Sub-Component: other
  • Priority: P3
  • Status: Closed
  • Resolution: Delivered
  • Fix Versions: 16
  • Submitted: 2018-07-23
  • Updated: 2024-08-14
  • Resolved: 2021-01-28
Related Reports
Blocks :  
Blocks :  
Blocks :  
Blocks :  
Blocks :  
Blocks :  
Blocks :  
Blocks :  
Blocks :  
Blocks :  
Blocks :  
Blocks :  
Blocks :  
Blocks :  
Blocks :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Sub Tasks
JDK-8211666 :  
JDK-8211667 :  
JDK-8211668 :  
JDK-8211669 :  
Description
Summary
-------

Allow the use of C++14 language features in JDK C++ source code, and give
specific guidance about which of those features may be used in HotSpot code.

Goals
-----

Through JDK 15, the language features used by C++ code in the JDK
have been limited to the C++98/03 language standards.
With JDK 11, the code was updated to support building with newer
versions of the C++ standard, although it does not yet use any new
features. This includes being able to build with recent versions of
various compilers that support C++11/14 language features.

The purpose of this JEP is to formally allow C++ source code changes
within the JDK to take advantage of C++14 language features, and to give
specific guidance about which of those features may be used in HotSpot
code.

Non-Goals
----------

This JEP does not propose any usage or style changes for C++
code in the JDK that is outside of HotSpot. The rules are already different
for HotSpot and non-HotSpot code. For example, C++ exceptions are
used in some non-HotSpot code, but are disallowed in HotSpot by
build-time options. However, build consistency requirements will make
the newer language features available for use in all C++ code in the JDK.

Description
-----------

### Changes to the Build System

To take advantage of C++14 language features some build-time
changes are required, with specifics depending on the platform compiler. Minimum
acceptable versions of the various platform compilers also need to be
specified. The desired language standard should be specified
explicitly; later compiler versions might default to a later, and
possibly incompatible, language standard.

* Windows: Visual Studio 2017 is required for JDK 11. (Earlier versions
  will generate a configure-time warning and may or may not work.) For
  Visual Studio 2017 the default C++ standard is C++14. The `/std:c++14`
  option should be added.  Support for older versions will be dropped
  entirely.

* Linux: Replace the `-std=gnu++98` compiler option with `-std=c++14`.
  The minimum supported version of gcc is 5.0.

* macOS: Replace the `-std=gnu++98` compiler option with `-std=c++14`.
  The minimum supported version of clang is 3.5.

* AIX/PowerPC: Replace the `-std=gnu++98` compiler option with
  `-std=c++14` and require the use of xlclang++ as the compiler.  The
  minimum supported version of xlclang++ is 16.1.

### Changes to C++ Usage in HotSpot code

The existing restrictions and best-practice recommendations for C++ usage in
HotSpot code are based on the C++98/03 language standard, and described in the
[HotSpot Style Guide](https://wiki.openjdk.java.net/display/HotSpot/StyleGuide).

We will add similar restrictions and guidelines for features of more
recent language standards to that document.  They will be described by a
table of permitted features and another of excluded features.  Use of
permitted features may be unconditional or may have some restrictions or
additional guidance.  Use of excluded features is forbidden in HotSpot
code.

There is a third category, undecided features, about which HotSpot
developers have not reached a consensus, or possibly discussed at all.
Use of these features is also forbidden.

Note, however, that the use of some language features may not be
immediately obvious and may accidentally slip in anyway, since the
compiler will accept them. As always, the code review process is the
main defense against this.

Proposed changes to a feature's categorization are approved by
[rough consensus](https://en.wikipedia.org/wiki/Rough_consensus)
of the HotSpot Group members, as determined by the Group Lead.
Such changes must be documented in updates to the Style Guide.

Lists of new features for C++11 and C++14, along with links to their
descriptions, can be found in the online documentation for some of the
compilers and libraries:

* [C++ Standards Support in GCC](https://gcc.gnu.org/projects/cxx-status.html)
* [C++ Support in Clang](https://clang.llvm.org/cxx_status.html)
* [Visual C++ Language Conformance](https://docs.microsoft.com/en-us/cpp/visual-cpp-language-conformance)
* [libstdc++ Status](https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html)
* [libc++ Status](https://libcxx.llvm.org/cxx1y_status.html)

As a rule of thumb, permitting features which simplify writing code and,
especially, reading code, are encouraged.

HotSpot has largely avoided using the C++ Standard Library.  Some of
the reasons for that may be obsolete (in particular, bugs encountered
in early versions), while others may still be applicable (minimizing
dependencies). Categorizing pieces of the Standard Library should go
through the same process as language features.

An initial set of feature categorizations for HotSpot follows.

### Permitted

* `constexpr`
    * Relaxing requirements on `constexpr` functions
    ([n3652](https://isocpp.org/files/papers/N3652.html))
    * Generalized constant expressions
    ([n2235](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2235.pdf))

    Relaxed `constexpr` (n3652) is perhaps the key feature distinguishing C++14 from
    C++11.  The `constexpr` feature will permit the elimination of some clumsy macro code
    in favor of `constexpr` functions. This feature is also the foundation for simplified
    metaprogramming idioms.  See [mpl11][1] and [mpl11_2][2].

* Sized deallocation
([n3778](https://isocpp.org/files/papers/n3778.html)) —
Syntax is already in use, because of Visual Studio.

* Variadic templates
    * Variadic templates
    ([n2242](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2242.pdf))
    * Extending variadic template template parameters
    ([n2555](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2555.pdf))

    Probably only occasionally useful directly, but foundational for simplified
    metaprogramming idioms such as described in [mpl11][1] and [mpl11_2][2].

* Static assertions
([n1720](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1720.html)) —
Replaces HotSpot STATIC_ASSERT macro, providing better error messages.

* `decltype`
    * Declared type of an expression
    ([n2343](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2343.pdf))
    * `decltype` and call expressions
    ([n3276](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3276.pdf))

    Important metaprogramming tool.  Needed to implement a simplified
    function template SFINAE utility.

* Right angle brackets
([n1757](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1757.html)) —
Eliminates an annoying syntactic wart.

* Default template arguments for function templates
([CWG D226](http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#226)) —
In addition to being useful in its own right, this is needed to
implement a simplified function template SFINAE utility.

* Template aliases
([n2258](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2258.pdf)) —
Typedefs with template parameters.  Provides syntax for partial specializations of a
class template, rather than using inheritance (in sometimes inappropriate ways).
Also used as metafunctions in simplified approach to metaprogramming [mpl11][1]
and [mpl11_2][2].

* Strongly-typed enums
([n2347](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2347.pdf)) —
Allows explicit control of the underlying type for an enum, rather than leaving it
potentially implementation defined (and varying between implementations).  Also
allows strong typing for enum classes, eliminating implicit conversions.  It is
recommended that _scoped-enums_ be used when the enumerators are indeed
a logical set of values.  Use of _unscoped-enums_ is permitted, though ordinary
constants should be preferred when the automatic initializer feature isn't used.
The _enum-base_ should always be specified explicitly, rather than leaving it
dependent on the range of the enumerator values and the platform.

* Delegating constructors
([n1986](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1986.pdf)) —
Eliminate some code duplication and simplification by allowing specialized constructors
to chain to more general constructors.

* Explicit conversion operators
([n2437](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2437.pdf)) —
Use to make some existing conversion operators safe.

* Standard Layout Types
([n2342](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2342.htm))

* Defaulted and deleted functions
([n2346](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm))

* Dynamic initialization and destruction with concurrency
([n2660](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2660.htm)) —
Thread-safe function-local statics.

* `<type_traits>` is a core metaprogramming library. It
eliminates the need for many of the HotSpot metaprogramming utilities,
which were modeled on corresponding parts of this library.

* `final` virtual specifiers for classes and virtual functions
([n2928](http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2928.htm)),
([n3206](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3206.htm)),
([n3272](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3272.htm)) &#x2014;
The `final` specifier permits devirtualization of virtual function
calls.  This can provide better performance than relying on the
compiler's use of techniques such as points-to analysis or speculative
devirtualization.  The `overrides` specifier for virtual functions,
which is also described in the referenced papers, may be considered at
a later time.

* Local and unnamed types as template parameters
([n2657](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm)) &#x2014;
Allows local definition of used-once helper classes to be placed near
the point of use when the use is a template, rather than requiring
such helpers be defined at namespace scope.

* `nullptr` and `std::nullptr_t`
([n2431](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2431.pdf))

* `auto` variable declarations
([n1984](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1984.pdf)) &#x2014;
Use only when it makes the code clearer or safer.  Do not use it merely to avoid the
inconvenience of writing an explicit type, unless that type is itself difficult to write.
For local variables, this can be used to make the code clearer by eliminating type
information that is obvious or irrelevant.  Excessive use can make code much harder
to understand.

* Function return type deduction
([n3638](https://isocpp.org/files/papers/N3638.html)) &#x2014;
Only use if the function body has a very small number of `return`
statements, and generally relatively little other code.

* Expression SFINAE
([n2634](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2634.html))

### Excluded

*   New string and character literals
    * New character types
    ([n2249](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2249.html))
    * Unicode string literals
    ([n2442](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2442.htm))
    * Raw string literals
    ([n2442](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2442.htm))
    * Universal character name literals
    ([n2170](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2170.html))

    HotSpot doesn't need any of the new character and string literal
    types.

*   User-defined literals
    ([n2765](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2765.pdf)) &#x2014;
    User-defined literals should not be added casually, but only
    through a proposal to add a specific UDL.

*   Inline namespaces
    ([n2535](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2535.htm)) &#x2014;
    HotSpot makes very limited use of namespaces.

*   `using namespace` directives.  In particular, don't use `using
    namespace std;` to avoid needing to qualify Standard Library names.

*   Propagating exceptions
    ([n2179](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2179.html)) &#x2014;
    HotSpot does not permit the use of exceptions, so this feature isn't useful.

*  `thread_local`
([n2659](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2659.htm)) &#x2014;
Use HotSpot `THREAD_LOCAL` macro; see discussion for
[JDK-8230877](https://mail.openjdk.java.net/pipermail/hotspot-dev/2019-September/039487.html).

* `<atomic>`
([n2427](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2427.html)),
([n2752](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2752.htm));
instead, use the HotSpot `Atomic` class and related facilities.

* `[[deprecated]]` attribute
([n3760](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3760.html)) &#x2014;
Not relevant in HotSpot code.

[1]: http://www.pdimov.com/cpp2/simple_cxx11_metaprogramming.html "Simple C++11 metaprogramming"
[2]: http://www.pdimov.com/cpp2/simple_cxx11_metaprogramming_2.html "Simple C++11 metaprogramming, part 2"

### Similar lists for some other projects

*   [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html) &#x2014;
    Currently targeting C++11 and should not use C++14

*   [C++11 and C++14 use in Chromium](https://chromium-cpp.appspot.com) &#x2014;
    Categorizes features as allowed, banned, or to be discussed.

*   [llvm Coding Standards](https://llvm.org/docs/CodingStandards.html) &#x2014;
    Currently targeting C++11 and should not use C++14.

*   [Using C++ in Mozilla code](https://developer.mozilla.org/en-US/docs/Mozilla/Using_CXX_in_Mozilla_code) &#x2014;
    C++14 support is required.


Risk and Assumptions
--------------------

There may be other platforms with toolchains that do not yet support the
C++14 language standard.

There may be bugs in the support for some new features by some compilers.

Comments
> The overrides specifier for virtual functions, which is also described in the referenced papers, may be considered at a later time. The override specifier is already in some way "enforced" by clang, clang 14/15 generate warnings as errors when the override specifier is missing. See for example JDK-8338236 .
14-08-2024

Added Expression SFINAE to the Permitted list. That's the only discrepency I found between here and the updated HotSpot Style Guide document.
10-07-2020

[~mr] OK. Done. I think "Adopt" made more sense when this was mostly about HotSpot, but with the expanded scope of the description, those changes make sense.
09-07-2020

[~kbarrett] Thanks for the background; I’ve now read the review thread. I think the root of my confusion is the use of the word “adopt” in the title. This JEP isn’t about adopting C++14 language features, but rather enabling their use and giving guidance about which ones may be used in HotSpot. I’ll therefore suggest that a better title would be “Enable C++14 Language Features” (there’s no need to say “in the JDK” -- that’s implicit). I’ll also suggest moving the parenthetical comment about not proposing usage guidance for non-HotSpot up into the Non-Goals section, which will make it more prominent.
09-07-2020

[~mr] The scope in the title and summary were expanded by request of a non-HotSpot reviewer who wanted it to be clear the effect of allowing the use of C++14 features applied to all of the JDK and not just to HotSpot. The point being that other parts of the JDK are ready and eager to start using C++14 features once the necessary build changes have been made. See the review thread: https://mail.openjdk.java.net/pipermail/hotspot-dev/2020-June/041999.html and in particular the part starting here: https://mail.openjdk.java.net/pipermail/hotspot-dev/2020-June/042025.html I've adjusted the Summary and Goals. Changing the title back to saying "HotSpot" instead of "the JDK" would go directly against the reviewer's request.
09-07-2020

[~kbarrett] I’m a little confused about the scope of this JEP. You recently changed the title from “Adopt C++14 Language Features in HotSpot” to “... in the JDK.” Your focus, however, is on changes in HotSpot code and the HotSpot style guide, including detailed rules about which C++14 features may be used in HotSpot. A side effect of the related build-system changes is that code outside of HotSpot will be able to use C++14 features, but the HotSpot style rules won’t apply there, as you mention parenthetically. To make all this clearer up front, consider changing the title back to “Adopt C++14 Language Features in HotSpot,” and elaborating the Summary and Goals sections to mention revising the HotSpot style guide with lists of permitted and excluded C++14 features. A new Summary might be something like this: “Allow the use of C++14 language features in the JDK, and give specific guidance about which of those features may be used in HotSpot.”
07-07-2020

I agree with Jesper that the main concerns about publishing a Java style guide don’t apply to this C++ style guide. I agree with Kim, however, that jdk/jdk/doc is the best place we have to put this guide right now, for a couple of reasons: - This guide is version-bound -- it doesn’t apply to JDK 15 or earlier, and a future version that permits the use of, say, C++20 features won’t apply to JDK 16. I doubt that we want the Developers’ Guide to contain multiple versions of this guide. - This guide will be maintained by a different set of rules than the Developers’ Guide, with a different set of approvers. If and when we have better JEP infrastructure then we can consider surfacing this guide as an informational JEP, but for now jdk/jdk/doc will do.
07-07-2020

The second has a pageId since the name of the page contains a /. As far as I understand the objections against placing the Java style guide in the Developers' Guide doesn't apply to the HotSpot C++ style guide. The problem with the Java guide is that if we publish such a guide it will get a lot of attention and it will be seen as *The* Java style guide by random Java programmers not associated with OpenJDK. This makes the procedures around that document more strict and it's also why there are people who simply don't want a Java style guide to be published by us. If the OpenJDK publish a C++ style guide it won't have the same effect on the C++ community. It's fairly unlikely that anyone outside the OpenJDK will care about our C++ guide. For that reason I don't think that this C++ guide will encounter the same opposition if proposed to be published in the Developers' Guide.
07-07-2020

There are currently 3 documents in jdk/jdk/doc: building.{md,html}, testing.{md,html}, ide.{md,html} These are all developer-focused documentation. I think the HotSpot Style Guide fits in well with this collection. The other two wiki pages associated with the current wiki HotSpot Style Guide would similarly fit well here: https://wiki.openjdk.java.net/display/HotSpot/Naming+HotSpot+JTReg+Tests https://wiki.openjdk.java.net/pages/viewpage.action?pageId=37584961 (And no, I don't know why the second has a pageId and not a name.) As for being published online, maybe not the most convenient access but they can be viewed via hg.openjdk.java.net and use the raw view of the .html versions, like: https://hg.openjdk.java.net/jdk/jdk/raw-file/25551ba96f75/doc/building.html Given that there seem to be folks who are dead set against style guides being in the Developers' Guide, I'm inclined to put it in jdk/jdk/doc.
07-07-2020

I agree with reducing the distance, but I don't think the JDK repository is the right place for this type of documentation. It's not JDK version bound in the same sense as documentation of features etc. It also has a different audience than the JDK documentation. The HotSpot Style Guide is meant to be used by developers building the JVM, not users of the JDK. There's also the question about how the documentation is accessed. If it's included in the JDK source tree, where will it be published online? I went through the same discussion regarding the Developers' Guide (being the same type of document) and the conclusion was that a separate source repo for the Guide would be more beneficial for the end users. The published Guide will get more frequent updates and won't be affected by release dates or need backports. I think the same is true for the HotSpot Style Guide. In fact (and I've said it elsewhere already) I think the Developers' Guide is a perfect host for the HotSpot Style Guide.
29-06-2020

The further the "distance" between doc and code, the more likely that developers don't maintain the doc. So put all developer-facing docs into the openjdk repo.... (unless it's not version-specific?)
28-06-2020

There is an open question about where the HotSpot Style Guide should be located. It is currently an OpenJDK wiki page, but there are on-going discussions about other possible locations. Suggestions include the new Developers' Guide, an Informational JEP, or the doc directory of the jdk repository. Resolution of this question needn't delay integration of the build changes. If no other decision is made in a timely fashion, the default will be to update the OpenJDK wiki page.
28-06-2020

got it. thanks!
10-06-2020

[~xliu] Thread-safe function scoped static initialization is n2660; already in the list.
10-06-2020

Another thing can ease hotspot developers is Meyer's singleton. It's not thread-safe until c++11. https://stackoverflow.com/questions/1661529/is-meyers-implementation-of-the-singleton-pattern-thread-safe
20-02-2020

Since JDK-8218965 we use xlclang++ by default on AIX. It uses a clang frontend with C++14 support. This should make AIX ready for this JEP. * C Compiler: Version 16.1.0 (at /opt/IBM/xlC/16.1.0/bin//xlclang) * C++ Compiler: Version 16.1.0 (at /opt/IBM/xlC/16.1.0/bin//xlclang++)
20-02-2019

Changed Scope to "Implementation" and added myself as Reviewer.
19-11-2018

One thing to consider is that code that may eventually be downported to an previous LTS release should not use such new features. Especially all security fixes should be explicitly disallowed to use theses new idioms.
04-10-2018