Software Bits Newsletter

Share this post
std::move
softwarebits.substack.com

std::move

Or explicit code FTW.

Taras Tsugrii
Dec 25, 2021
Comment
Share

std::move was probably one of the most confusing additions to C++ 11. It doesn’t even move anything and is just a glorified static_cast, after all. Because of this I always thought of it as a way to transfer (move) ownership, which is invaluable for communicating intent. Ownership transfer has an interesting side-effect though - it enables new owners to reuse the guts of “gifted” instance instead of using defensive copying. For example, in case of std::string, we can reuse the heap-allocated array of characters without unnecessary copies.

C++ moves for people who don't know or care what rvalues are 🏘️ | by  Topher Winward | Medium

Oh well, things are always great in theory, but what about practice? Let’s check

and the results do not disappoint and a cleaner version that explicitly releases ownership of the s after first copy is 1.4X faster than its copy-only sibling

I always recommend writing explicit code and now in addition to handwavy “clean code” argument I can also back up my recommendation with performance improvements.

CommentComment
ShareShare

Create your profile

0 subscriptions will be displayed on your profile (edit)

Skip for now

Only paid subscribers can comment on this post

Already a paid subscriber? Sign in

Check your email

For your security, we need to re-authenticate you.

Click the link we sent to , or click here to sign in.

TopNewCommunity

No posts

Ready for more?

© 2022 Taras Tsugrii
Privacy ∙ Terms ∙ Collection notice
Publish on Substack Get the app
Substack is the home for great writing