13

So there are copy semantics, move semantics and maybe more semantics that I don't know of. I have read articles about both but I still do not really get have a good definition of "semantics". As the name suggests, move semantics have something to do with moving things but why is it called move semantics?

The more clear version: What is the meaning of semantics in the context of programming? Example: Move and copy semantics.

  • 12
    "semantics" = "the meaning of". – Will Ness Jul 05 '17 at 12:04
  • 1
    https://en.wikipedia.org/wiki/Semantics#Computer_science – MB-F Jul 05 '17 at 12:06
  • @WillNess I don't get it. –  Jul 05 '17 at 12:06
  • semantic: *adjective* relating to meaning in language or logic. – Passer By Jul 05 '17 at 12:07
  • So move semantics is "the meaning of moving". As i sort of implied, I am very confused about this and therefore I probably need more the 4 words to understand. :/// –  Jul 05 '17 at 12:07
  • 4
    "move semantics" = "what is the meaning of moving something? how is it done? in what situations? what are its pre- and post-conditions? what does it *mean*, to "move" something?" – Will Ness Jul 05 '17 at 12:08
  • Syntax defines how to build correct sentances -> its like grama. Semantics answers question what does sentence mean. You could write (arguably) grammar correct language statements without knowing what they mean. So syntax is correct, semantics is not. Same applies to programming language. Read about programming languages in general to find out more. https://en.wikipedia.org/wiki/Programming_language_theory – cerkiewny Jul 05 '17 at 12:08
  • Btw I did actually read the wiki article and get that it represents some sort of logical meaning but couldn't still match it with "move semantics". –  Jul 05 '17 at 12:11
  • 5
    How is this question "too broad"? It has exactly one definitive answer. – Galik Jul 05 '17 at 12:24
  • so the problem here was that OP didn't provide actual sentences using that phrase they had trouble understanding. so thanks to the accepted answer, in the end it is about a specific convoluted programmers jargon of "X has move semantics", which in simple English could be said simply as "X is like moving"; or "Y has copy semantics" == "Y is like copying". i.e. the meaning of operation X (Y, ...) is like that of moving (copying, ...). – Will Ness Apr 18 '19 at 18:09
  • What most other answers and comments fail to emphasize is that the word **"semantics"** is most important when **when comparing an action in one language to a similar action in another language** (or perhaps sometimes in the same language but in a different context). For example, "move" may invoke a precise set of steps in one language, but in another language it does something (even slightly) different... even with the same syntax. When the *same* word (e.g. move, copy) is used to describe technically different operations, the semantics describe the details (or meaning) of those differences. – C Perkins May 23 '20 at 18:49

3 Answers3

18

Semantics basically means "the meaning of".

It may help to look at a more familiar case to explain the term.

Consider:

int a = 3;
int b = 5;

int c = a + b;

The value of a + b is 8 because the semantics of the + operator is to take the numerical sum of its operands.

Now consider this:

std::string a = "hello";
std::string b = "world";

std::string c = a + b;

The value of a + b is "helloworld" because the semantics of the + operator is to concatenate its operands.

The + operator, when used with std::string is said to have different semantics from when it is used with numerical types.

It has a different meaning.

Now consider copy and move semantics:

std::string a = "hello";
std::string b;

b = a; // b receives a copy of a's value

b = std::string("hello"); // the temporary value is moved into b

We have the same operator = which has a different meaning in different situations. Again we say is has different semantics.

The first case has copy semantics and the second case has move semantics.

Galik
  • 47,303
  • 4
  • 80
  • 117
  • Your examples could be extended to where `int operator+(int, int)` differs to `std::string operator+(std::string, std::string`, e.g. `int d = b + a; assert(c == d);` and `std::string d = b + a; assert(c != d)` – Caleth Jul 05 '17 at 12:24
  • Thanks. So "move semantics" simply means "things concerning movement"? Or is there something that I did not catch? –  Jul 05 '17 at 14:12
  • Excellent. This is my accepted answer. – Ignorant Nov 30 '19 at 17:55
7

The word semantics is used to describe an underlying meaning of something.

You can say that an operation has the move semantics when it transfers an object state from one object to another. In reality of course what happens is some pointers are probably copied over and that's it, but semantically your object has been moved.

Another example is the ownership transfer, when the most important thing that is moved is the responsibility (i.e. the promise to release some resource). In that case from the computational point of view pretty much nothing happens, but semantically the ownership is transferred.

The same goes for copy semantics: you can say that passing an object to a function has copy semantics i.e. your object would be duplicated and the function will get a standalone copy with its own lifetime.

Another side of the coin is the syntax which is how you describe what you want following the rules of the language.

C++ has really flexible syntax - overloading operators, user-defined conversions, macros and what not, so almost any desirable semantics can be attached to any particular syntax.

Ap31
  • 3,244
  • 1
  • 18
  • 25
3

Semantics is about the meaning of something. move-semantic is about the meaning of moving objects. Specifically, in this context, it tells you what it means to move something in C++. The semantic of moving is all about everything that is involved during a move operation.

read move-semantic as: what does it mean, what are the implications (and how do you achieve it ) of moving an object in a C++ program?

The following is a good answer that might help: What are move semantics?

Davide Spataro
  • 7,319
  • 1
  • 24
  • 36