RFR: 8314644: Change "Rvalue references and move semantics" into an accepted feature [v2]

Julian Waters jwaters at openjdk.org
Wed Dec 20 06:02:59 UTC 2023


On Tue, 24 Oct 2023 10:37:43 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
>
> Johan Sjölen has updated the pull request incrementally with two additional commits since the last revision:
> 
>  - Add a (admittedly clunky) single asterisk
>  - Expand on the feature

Shame that this never made it in, this sounds pretty useful

-------------

PR Comment: https://git.openjdk.org/jdk/pull/15386#issuecomment-1863901085


More information about the hotspot-dev mailing list