7

In Cpp, the "move semantics" of an object refers to the concept that "move but not copy the object to a newer", but the word "semantics" really confuse me.

What is the difference between the "semantics" and "function"? How to use this word correctly? If I realize a method called "max(A,B)", could we say "I realize a max semantic"? If I coding a object called "list", could we say "I realize a sequence storage semantic"?

mradwan
  • 83
  • 2
  • 2
  • 14
Jianjie
  • 107
  • 5
  • 4
    Semantics means "meaning". The point of "move semantics" is not what it does, but what it means. – Raymond Chen Sep 14 '21 at 14:23
  • In programming languages semantics is usually opposed to syntax. Syntax describes which sequences of characters are considered to be correct in other words belong to the language. Semantics describes what these "correct" sequences of characters mean. – Nolan Sep 14 '21 at 14:23
  • The _What does "semantics" mean_ part is a duplicate of [this question](https://stackoverflow.com/q/3355349/212858) IMO. I'm not voting to close because it doesn't cover move semantics, but you should have been able to find out what "semantics" means in general with no trouble. – Useless Sep 15 '21 at 09:29
  • @Useless thank you for your suggestions. It is strange that I had been failed to find what your referred before I asked. – Jianjie Sep 15 '21 at 09:33

1 Answers1

14

"Semantics" is the meaning or interpretation of written instructions. It is often contrasted to "syntax", which describes the form of the instructions. For example, an assignment foo = bar has the same form in many programming languages, but not necessarily the same meaning. In C++ it means copy, in Rust it means move, and in Java or Python it means copy the object reference.

"Move semantics" applied to C++ syntax such as assignment or passing parameters to functions means that that syntax is or can be interpreted as object move as opposed to object copy. Semantics is not a synonym for "function", so "max semantics" doesn't make much sense.

Other examples where the word can be applied is reference semantics as opposed to value semantics, or "short-circuit semantics" (of the && and || operators) as opposed to evaluating all clauses. Basically, anything where there are multiple possible meanings of what you wrote, and you need to name and pinpoint the correct one.

user4815162342
  • 141,790
  • 18
  • 296
  • 355