c++semantics

What does "semantics" mean? and why are "move semantics" named as such, instead of any other term?


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"?


Solution

  • "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 can mean copy, in Rust it can mean move, and in Java or Python it means copy the object reference.

    "Move semantics" applied to C++ constructs such as assignment or argument passing is the situation in which the syntax of those constructs is 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 eager evaluation. Basically, anything where there are multiple possible meanings of what you wrote, and you need to name and pinpoint the correct one.