RFR: 8314644: Change "Rvalue references and move semantics" into an accepted feature
Johan Sjölen
jsjolen at openjdk.org
Tue Sep 5 12:15:39 UTC 2023
On Tue, 22 Aug 2023 12:16:49 GMT, Johan Sjölen <jsjolen at openjdk.org> wrote:
> Hi,
>
> I'd like to propose that rvalue references and move semantics are now considered permitted in the style guide. This change would allow for move constructors to be written. This enables more performant code, if the move ctr is less expensive than the copy ctr, but also more correct code. For the latter part, look at "8314571: GrowableArray should move its old data and not copy it". Here we can avoid using copy assignment, instead using move constructors, which more accurately reflects what is happening: The old elements are in fact moved, and not copied.
>
> Two useful std functions will become available to us with this change:
>
> 1. `std::move`, for explicitly moving a value. This is a slightly more powerful `static_cast<T&&>(T)`, in that it also handles `T&` corectly.
> 2. `std::forward`, which simplifies the usage of perfect forwarding. Perfect forwarding is a technique where in copying is minimized. To quote Scott Meyers ( https://cppandbeyond.com/2011/04/25/session-announcement-adventures-in-perfect-forwarding/ ):
>
>> Perfecting forwarding is an important C++0x technique built atop rvalue references. It allows move semantics to be automatically applied, even when the source and the destination of a move are separated by intervening function calls. Common examples include constructors and setter functions that forward arguments they receive to the data members of the class they are initializing or setting, as well as standard library functions like make_shared, which “perfect-forwards” its arguments to the class constructor of whatever object the to-be-created shared_ptr is to point to.
>
> Looking forward to your feedback, thank you.
> Johan
> _Mailing list message from [daniel.daugherty at oracle.com](mailto:daniel.daugherty at oracle.com) on [hotspot-dev](mailto:hotspot-dev at mail.openjdk.org):_
>
> A couple of questions:
>
> - Are these C++ features supported in all of the compilers that we use/support ? in OpenJDK? I mean the Tier1 platforms that Oracle supports in our CI and the ? other platforms that OpenJDK supports in the GHA setups. - How do we gauge the quality of support for these C++ features in the set of ? compilers that we use/support in OpenJDK? - Is it possible, feasible and desirable to create standalone tests for these ? C++ features so that we can use the new tests to evaluate compiler upgrades?
>
> Dan
>
> On 8/22/23 8:23 AM, Johan Sj?len wrote:
Hi Dan, here are my findings:
>- Are these C++ features supported in all of the compilers that we use/support
Looking at: https://en.cppreference.com/w/cpp/compiler_support/11
We can confirm that Rvalue references are supported by gcc 4.5, clang 17 and MSVC (no version mentioned). For IBM XL for AIX it is supported according to this url: https://www.ibm.com/docs/en/openxl-c-and-cpp-aix/17.1.1?topic=features-supported-language-levels
To be clear: This is a feature from C++11 and is considered table stakes for C++ compilers today, I'd be surprised if it wasn't supported. But we don't want to be taken by surprise, I get that :-).
>How do we gauge the quality of support for these C++ features in the set of compilers that we use/support in OpenJDK?
>Is it possible, feasible and desirable to create standalone tests for these C++ features so that we can use the new tests to evaluate compiler upgrades
We may add tests checking that the move constructors are called when `std::move` is invoked of course, and this would also be some gauge of the quality of implementation in the compilers. I am not sure why this is needed however, we do not have tests checking that the compilers have compiled copy constructors correctly, for example. It would be more natural to write tests that check the capabilities of our classes that use move constructors. For example, in my recent CR 8314571 I added a test that checks that `GrowableArray` correctly handles a struct with only a move constructor. These tests would hopefully fail if a compiler upgrade breaks rvalue references. Reading the thread for 8274400 (which made usage of `alignof` acceptable), there were no mentions of adidng tests that ensure that `alignof` actually functions in our compilers.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/15386#issuecomment-1706505963
More information about the build-dev
mailing list