RFR: 8314644: Change "Rvalue references and move semantics" into an accepted feature [v2]
Johan Sjölen
jsjolen at openjdk.org
Tue Oct 24 10:37:43 UTC 2023
> 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
-------------
Changes:
- all: https://git.openjdk.org/jdk/pull/15386/files
- new: https://git.openjdk.org/jdk/pull/15386/files/a85b1d18..2cba5f23
Webrevs:
- full: https://webrevs.openjdk.org/?repo=jdk&pr=15386&range=01
- incr: https://webrevs.openjdk.org/?repo=jdk&pr=15386&range=00-01
Stats: 8 lines in 1 file changed: 8 ins; 0 del; 0 mod
Patch: https://git.openjdk.org/jdk/pull/15386.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/15386/head:pull/15386
PR: https://git.openjdk.org/jdk/pull/15386
More information about the hotspot-dev
mailing list