Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the origin of move semantics in C++?

I am wondering what is the origin of move semantics in C++? In particular was it invented specifically for this language or there was something similar in other language(s)? In the latter case could you give some references.

like image 728
vitaut Avatar asked Feb 14 '12 20:02

vitaut


People also ask

What is a move semantic?

Move semantics is a set of semantic rules and tools of the C++ language. It was designed to move objects, whose lifetime expires, instead of copying them. The data is transferred from one object to another. In most cases, the data transfer does not move this data physically in memory.

Does C have move semantics?

One of the most important concepts introduced in C++11 was move semantics. Move semantics is a way to avoid expensive deep copy operations and replace them with cheaper move operations.

Are move semantics important?

A little bit about std::moveMove Semantics is an extremally important concept for one to understand talking about programming in c++. It is a fundamental aspect of the language that may not be that obvious and even more for one coming from another language such as Swift, C#, or Java.

What is the point of STD move?

std::move is used to indicate that an object t may be "moved from", i.e. allowing the efficient transfer of resources from t to another object. In particular, std::move produces an xvalue expression that identifies its argument t . It is exactly equivalent to a static_cast to an rvalue reference type.


2 Answers

There doesn't appear to be any kind of specific ancestor to the concept. The origin of C++'s move semantics, as noted in the original proposal, was discussion in newsgroups:

Move semantics in various forms has been discussed in C++ forums (most notably comp.lang.c++.moderated) for years.

To my mind, they are tightly coupled with C++'s notion of lvalues and rvalues which, if I'm not mistaken, is purely a C++ concept. A language that doesn't have lvalues, rvalues and their new C++11 friends doesn't need move semantics in the way that C++ implements them.

More generally, though, the concept of moving stuff around rather than copying is just a fundamental concept. Whenever you write a linked list and you "swap elements" by actually just swapping pointers to them, you're doing a "move". Basically.

like image 177
Lightness Races in Orbit Avatar answered Sep 28 '22 12:09

Lightness Races in Orbit


You can read "A Proposal to Add Move Semantics Support to the C++ Language" to get more information on the motivation behind the concept, as well as why this needs to have direct language support rather than being implemented using library facilities.

like image 22
André Caron Avatar answered Sep 28 '22 13:09

André Caron