What are semantics?

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

The clearer version: what is the meaning of semantics in a programming context? Example: moving and copying semantics.

+3


source to share


3 answers


The word semantics is used to describe the basic meaning of something.

An operation can be said to have move semantics where it transfers the state of an object from one object to another. In fact, of course, some pointers are probably copied and what it is, but semantically your object has been moved.

Another example is the transfer of property rights, where the most important driving force is responsibility (i.e. a promise to free some resource). In this case, from a computational point of view, almost nothing happens, but property is transferred semantically.



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

The other side of the coin is syntax which describes what you want, following the rules of the language.

C ++ has really flexible syntactic - overloading operators, custom conversions, macros and what not - so almost any desired semantics can be tied to any particular syntax.

+2


source


Semantics basically means meaning.

It might 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 a + b

is equal 8

because the semantics of an operator +

must accept the numerical sum of its operands.

Now consider the following:

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

std::string c = a + b;

      



The value a + b

is equal "helloworld"

because the semantics of an operator +

is the concatenation of its operands.

The operator +

, when used with std::string

, is said to have different semantics when used with numeric types.

It has a different meaning .

Now let's look at the semantics of copy and move:

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

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

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

      

We have the same operator =

with different meaningsin different situations. Again we say that it has different semantics .

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

+4


source


Semantics about the meaning of something. move-semantic

refers to meaning

objects of movement . Specifically in this context, it tells you what something means move

in C ++. Moving semantic

is all about the operation move

.

read move-semantic

how: what does it mean, what are the consequences (and how do you achieve it) of moving an object in a C ++ program?

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

+1


source







All Articles