Words with a double meaning: definition, examples of use. Multiple measurements of the same quantity in the same way can give different values. Examples of polysemantic nouns



vote usa (6)

Is there a compilation way to detect/prevent duplicate values ​​in a C/C++ enum?

The catch is that there are several elements that are initialized with explicit values .

Background:

I have inherited some C code like:

#define BASE1_VAL (5 ) #define BASE2_VAL (7 ) typedef enum ( MsgFoo1A = BASE1_VAL , // 5 MsgFoo1B , // 6 MsgFoo1C , // 7 MsgFoo1D , // 8 MsgFoo1E , // 9 MsgFoo2A = BASE2_VAL , // Uh oh !7 again... MsgFoo2B // Uh oh!8 again... ) FOO ;

The problem is that as the code grows and as developers add more messages to the MsgFoo1x group, it eventually overflows BASE2_VAL .

This code will eventually be ported to C++, so if there's only a C++ solution (template magic?), that's fine - but a solution that works with C and C++ is better.

There are several ways to check this compile time, but they may not always work for you. Start by inserting the marker value "marker" right before MsgFoo2A.

typedef enum ( MsgFoo1A = BASE1_VAL , MsgFoo1B , MsgFoo1C , MsgFoo1D , MsgFoo1E , MARKER_1_DONT_USE , /* Don't use this value, but leave it here. */ MsgFoo2A = BASE2_VAL , MsgFoo2B ) FOO ;

Now we need a way to ensure that MARKER_1_DONT_USE< BASE2_VAL во время компиляции. Есть два распространенных метода.

Arrays of negative size

Error declaring an array with a negative size. It looks a little ugly, but it works.

extern int IGNORE_ENUM_CHECK [ MARKER_1_DONT_USE > BASE2_VAL ? - eleven ];

Almost every compiler ever written generates an error if MARKER_1_DONT_USE is greater than BASE_2_VAL. GCC spits out:

test. c : 16 : error : size of array ‘ IGNORE_ENUM_CHECK ’ is negative

Static assertions

If your compiler supports C11, you can use _Static_assert . Support for C11 is not ubiquitous, but your compiler may support _Static_assert anyway, especially since the corresponding function in C++ is widely supported.

_Static_assert(MARKER_1_DONT_USE< BASE2_VAL , "Enum values overlap." );

GCC issues the following message:

test. c : 16 : 1 : error : static assertion failed : "Enum values ​​overlap." _Static_assert(MARKER_1_DONT_USE< BASE2_VAL , "Enum values overlap." ); ^

Another approach might be to use something like gccxml (or more conveniently pygccxml) to identify candidates for manual inspection.

I'm not aware of anything that will automatically check all enum members, but if you want to check that future changes to initializers (or the macros they rely on) don't cause conflicts:

switch (0 ) ( case MsgFoo1A : break ; case MsgFoo1B : break ; case MsgFoo1C : break ; case MsgFoo1D : break ;

will result in a compiler error if any of the integral values ​​are reused, and most compilers will even tell you which value (numeric value) was the problem.

I don't believe there is a way to detect this with the language itself, given that there are conceivable cases where you want two enum values ​​to be the same. However, you can always make sure that all explicitly given items are at the top of the list:

typedef enum ( MsgFoo1A = BASE1_VAL , // 5 MsgFoo2A = BASE2_VAL , // 7 MsgFoo1B , // 8 MsgFoo1C , // 9 MsgFoo1D , // 10 MsgFoo1E , // 11 MsgFoo2B // 12 ) FOO ;

As long as the assigned values ​​are at the top, no collision occurs unless for some reason the macros expand to values ​​that are the same.

Usually this problem is overcome by providing a fixed number of bits for each group of MsgFooX and ensuring that each group does not overflow, it allocates the number of bits. The "number of bits" solution is good because it allows bitwise testing to determine which message group something belongs to. But there is no built-in language feature for this, because there are legitimate cases for an enum having two identical values:

typedef enum ( gray = 4 , //Gry should be the same gray = 4 , color = 5 , //Also makes sense in some cases couleur = 5) FOO ;

While we don't have full reflection, you can solve this problem if you can recover the enum values.

Somewhere it's declared:

enum E ( A = 0 , B = 0 );

elsewhere we build this mechanism:

template< typename S , S s0 , S ... s >struct first_not_same_as_rest : std::true_type(); template< typename S , S s0 , S s1 , S ... s >struct first_not_same_as_rest : std::integral_constant< bool , (s0 != s1 ) && first_not_same_as_rest < S , s0 , s ... >::value>(); template< typename S , S ... s >struct is_distinct : std::true_type(); template< typename S , S s0 , S ... s >struct is_distinct : std::integral_constant< bool , std :: is_distinct < S , s ...>:: value && first_not_same_as_rest< S , s0 , s ... >::value>();

Once you have this hardware (which requires C++11), we can do the following:

static_assert ( is_distinct< E , A , B >:: value , "duplicate values ​​in E detected");

and at compile time we guarantee that there are no two elements.

This requires O(n) recursion depth, and O(n^2) runs the compiler at compile time, so for extremely large enums, this can cause problems. AO(lg(n)) and O(n lg(n)) work at a much larger constant factor, can be done by sorting the list of elements first, but it's much more.

With the enum conversion code suggested for C++1y-C++17, this will be doable without repeating elements.

I didn't like any of the answers already posted here, but they gave me some ideas. The crucial method is to use Ben Voigt's answer on using a switch statement. If multiple cases in the switch have the same number, you will get a compilation error.

Most useful to me and possibly the original poster, this doesn't require any C++ capabilities.

To clear things up, I used aaronps answer: How can I avoid repeating myself when creating a C++ enum and dependent data structure?

First define it in some header somewhere:

#define DEFINE_ENUM_VALUE (name , value ) name = value , #define CHECK_ENUM_VALUE (name , value ) case name : #define DEFINE_ENUM (enum_name , enum_values ​​) \ typedef enum ( enum_values ​​(DEFINE_ENUM_VALUE ) ) enum_name ; #define CHECK_ENUM (enum_name , enum_values ​​) \ void enum_name ## _test (void) ( switch(0) ( enum_values(CHECK_ENUM_VALUE); ) )

Now when you need to have an enum:

#define COLOR_VALUES (GEN ) \ GEN (Red , 1 ) \ GEN (Green , 2 ) \ GEN (Blue , 2 )

Finally, these lines are required for the actual enumeration:

DEFINE_ENUM (Color , COLOR_VALUES ) CHECK_ENUM (Color , COLOR_VALUES )

DEFINE_ENUM creates an enum data type. CHECK_ENUM performs a test function that includes all enum values. The compiler will crash when compiling CHECK_ENUM if you have duplicates.

Let's analyze the current situation.

First, let's find out why different ways of measuring the same height led to different results.

At first glance, the first method is the most reliable. We apply a tape measure to the surface of the building and determine the desired height. A closer analysis shows that this is not entirely true. It turns out that the building has a slight slope, and the wall in the place where the measurements are made has a certain curvature - it is convex, and towards the street. This means that we did not measure the height of the building, but the length of the wall related to the height.

The second way is an indirect measurement. By measuring the fall time of the ball, we calculate the height using the well-known formula for free fall: h = gt 2 /2. This time the measurement is really about height. But, we forgot that the ball moves in the air and, therefore, experiences the resistance of the medium. Therefore, the value calculated by the formula is also not the true value of the height of the building.

The third dimension, like the second, is indirect. The height is determined from geometric considerations: in right triangle the length of the opposite leg is equal to the product of the length of the adjacent leg and the tangent of the angle. In our case, the height plays the role of one leg, and the distance from the laser to the building plays the role of another. This time we were let down by the assumption of a perfectly horizontal surface on which the building stands. The result - again measured a value that is not height, but now for a different reason.

So, in each method there are some fixed factors(in each case, their own, and there may be several), which lead to the appearance systematic error measurements in this way. Each time the value of the same quantity is measured under the same conditions, the systematic error has the same value. If these factors are taken into account by introducing the appropriate corrections, then it is possible to approach the real value of the measured quantity, and then the results of measurements by different methods (taking into account corrections for systematic error) can turn out to be quite close. Thus, in principle, systematic errors can be taken into account and even excluded , although this can be quite a challenge in practice.

Now let's try to find out why repeated measurements of the same height in the same way (including the same set of instruments) can lead to different values. It is associated with a number of random factors. In the considered example, there may be small mechanical vibrations soil, building and appliances, thermal effects associated with a change in the linear dimensions of the wall and the appliances used, etc. Finally, there is also the human factor associated with the perception of ongoing processes and the reaction to this perception. As a result, repeated measurements of the same quantity may result in different values ​​associated with random errors. From measurement to measurement, a random error can change both its sign and its magnitude. Due to the random nature of the effects it is impossible to predict the magnitude of such an error in advance .



Our analysis raises legitimate questions:

1. What is the "true" value of the measured quantity?

2. How to present measurement results taking into account errors?

Since these questions concern not only the example considered, but

and any other measurements, we will move on to generalizations and the development of general recommendations.

This particular example has shown common property, characteristic of any measurements, – any measurement is accompanied by errors .

This property, ultimately, is due to the fact that any measurement involves a certain interconnected chain of participants in the measurement procedure: observer - measuring device - analyzed object - "external environment".

The elements of this chain are connected huge amount interactions and movements. During the measurement process, the analyzed object, the measuring instrument, and the observer may be subject to various influences (including mutual ones), which affects the measurement result.

Of course, if we reduce influences that are not directly related to the measurement procedure, and try to take into account irremovable influences, then the accuracy of our measurements will increase. But an absolutely accurate measurement is impossible in principle. And this is largely due to the nature of the measured quantities themselves.



If, for example, we want to absolutely accurately measure the length of a metal rod, then we will find the presence of fundamentally irremovable (albeit very small) vibrations of the crystal lattice. There is no absolutely exact "true" length for the rod. It constantly changes randomly, deviating in one direction or another from some most frequently occurring value. We can take this value as the “true” value of the length and subsequently operate with it, talking about the length of the rod, or using this value for any calculations, for example, to determine the volume of the rod.

This kind of situation is found in many other dimensions. The measured quantities themselves can change randomly, which is due, as already mentioned above, to the physical nature of these quantities. Thus, we are faced with fundamental irremovability of random factors . They can be minimized, but they cannot be completely eliminated. Hence, when presenting the results of measurements, we must give information regarding our estimate of the "true" value of the quantity, taking into account random measurement errors (provided that the systematic error is excluded or taken into account in the form of an appropriate correction). It is clear that such information can be presented most completely from the results of multiple measurements.

Not uncommon in Russian. Very often, one and the same word can be called and / or characterize completely different objects or phenomena. Such words have one main meaning - the original, literal, and one (or more) - figurative, figurative, metaphorical. The latter usually arises on the basis of some feature, similarity, association.

Examples of polysemantic nouns

Among nouns, you can find a lot of examples of words with a double meaning. Here are just a few of them:

Word direct meaning Figurative meaning
Ticket A plane or train ticket, a theater or cinema ticket. Examination ticket.
Crest Combing tool, hairbrush. The crest of a wave or mountain.
Word speech unit. literary genre. For example, "The Tale of Igor's Campaign".
Hand Part of the body - right hand, left hand.
  • Position, position of a person - "He is my right hand."
  • "Handwriting", manner of execution, recognizable author's touch - "the hand of a great artist".
  • Physical strength - "heavy hand".
Brush The hand is the part of the body from the wrist to the fingertips. Paint tool.
Work Physical labor, effort, human occupation. The visible result of physical labor is "Good job!".
Sheet A leaf growing on a tree. Sheet of paper, notebook or landscape sheet.
Root Tree root. The part of the tree that is underground.
  • The mathematical root of a number. For example, the root of 4 is 2.
  • The cause of some phenomenon or event is the "root of evil", "the root of problems".
Duty A sum of money or material value promised by one person to another, the result of borrowing. A moral desire for something, a moral duty.

This is not the whole list. It is probably simply impossible to compose everything, because there are almost as many words with a double meaning in Russian as there are single-valued ones.

Examples of polysemous adjectives

Miscellaneous items one word can not only name, but also characterize. Here are some examples of such words:

Word direct meaning Figurative meaning
Steel Made from steel. For example, a steel knife. Very strong, unshakable - "nerves of steel".
Gold Made of gold - "gold earrings", "golden necklace". Very valuable, kind, possessing outstanding moral character- "golden man", "golden child", "golden heart".
Heavy Taking a large amount of physical effort - "hard work". About something that is difficult for others to endure - a "heavy person", a "heavy character".
White White - "white snow", "white sheet". A poem without rhyme is "blank verse".
Black Black color - "black eyes", "black marker". Angry, sarcastic, touching on sensitive topics in a rude way - "black humor", "black comedy".

Again, the list is incomplete. In addition, the list of words with a double meaning can include adjectives that simultaneously describe colors, smells and / or tastes: orange, raspberry, lemon, plum, and so on.

Examples of polysemantic verbs

Action words can also have more than one meaning:

Word direct meaning Figurative meaning
sit down Sit on a chair, in an armchair, on a horse. Get on the train (not literally sit on the roof of the train, but figuratively - take your place in it).
Get off / get off You can get off the train, get off at the desired stop, go to the store. "Go crazy/go crazy"
Beat Strike. "The spring is a fountain", "life is in full swing."
Cut Separate into pieces with a knife or other sharp blade. Cause an unpleasant sensation - "light hurts the eyes", "sound hurts the ear".

Most often, words with a double meaning are native Russian words. Borrowed terms usually have the same meaning.

Differences from homonyms

It is very important to distinguish words with a double meaning from homonyms: different words that are spelled the same. At polysemantic words there is a direct, basic meaning, and transferred on some basis. Homonyms have independent meanings. "handle" (door) and "handle" (writing) are homonyms, since there is no connection between them. But the word "satellite" is ambiguous - the celestial body was called "satellite" because it moves around the planet, like a human satellite.

From the very definition of a sign, it is already clear that its main characteristic is its inherent representative function: to be a representative, or a substitute, in a given language of some (certain) object. And this - meaning sign. The meaning of word marks can be objects in broad sense words - everything that can be somehow singled out and named, about which something is affirmed or denied. It should be noted that the values ​​are, first of all, objects of extralinguistic reality - natural and social. Another essential characteristic of a verbal sign is its meaning. Meaninglinguistic expression- this is the verbal formalized information associated with it, which allows you to distinguish the object it represents (or a set of objects of the same type) among other objects. For example, the meaning of the word "Moon" - in its usual use - may be such a characteristic of it as "a natural satellite of the Earth"; the meaning of the German sentence "DerSchneeistwei" in Russian is reproduced by the sentence "Snow is white"; the meaning of the word "theft" is "secret theft of someone else's property", etc.

Note that for the same object (or a set of objects) different distinguishing characteristics are possible. This means that two different expressions can have different meanings but the same meaning, such as "equiangular triangle" and "equilateral triangle". Words (or phrases) with the same meaning are called equivalent.In addition, the same word can have several meanings and, therefore, express different concepts(meanings). Such a phenomenon is called ambiguity. The ambiguity of words is inappropriate in scientific and professional communication.

Meaning is the link between the word and the object it denotes. Giving meaning to some language expression is an important logical way of introducing new terms into the language and clarifying the meanings of words already in it.

When we talk about meaning, we mean straight the meaning of words and phrases, in contrast, for example, from indirect, figurative (“white gold”, “black gold”, “flies on the wings of love”, and other metaphorical expressions that indicate only a certain similarity of some objects, processes, phenomena with others). The direct meaning must also be distinguished from the "literal" or etymological meaning ("geography" literally means describing the Earth, "lie" literally means "to speak", "talk", etc.).

With regard to meaning and meaning in logic, it is customary to assume that the meaning of a sign is a function of its meaning. This emphasizes the special role of meaning: it unambiguously points to the object denoted by the sign, mentally highlighting it from many others.

It is clear that both society and each individual person must have a certain stock of words that are correlated with their meanings without the mediation of meaning. Here we have the use of words as signs, the connection with the meanings of which is established in the process of pronouncing the word and the simultaneous sensory perception of its meaning, for example, color (“red”), smell, spatial configuration of the designated object, etc.

Everyone knows the division of natural language expressions into parts of speech. In logical "grammar" there is a similar subdivision, but on a different basis, namely, depending on the type of objects of thought represented by words (or phrases).

The first type of objects will include single items. We will consider as single objects such objects of knowledge, each of which has an individual difference from objects of the same type with it: the number 7, the marriage of A.S. Pushkin, Luna, etc. The logical category of linguistic expressions corresponding to single objects is single names. Their meaning is the information associated with them, which makes it possible to unambiguously distinguish this single object from a multitude of objects of the same type with it. Examples of such names: “Peter 1”, “The current President of the Russian Federation”, “Author of the novel “Eugene Onegin”, “Celebration of the 66th anniversary of the Victory over Nazi Germany" etc. Single names are subdivided into descriptive (complex) and on non-descriptive (simple) names. Examples of simple (non-descriptive) names are the words "Everest", "Yu.A. Gagarin", examples of complex (descriptive) names - "The first pilot-cosmonaut", "The largest river in Europe".

The second type of objects is the properties of objects and the relationships between them. Expressions that represent objects of this kind in the language, we will call universals. Examples of universals: the word "table" in the statement "This table is round"; the word "brother" in the statement "Ivan is the brother of Peter"; the word "crime" in the statement "Theft is a crime." The universal is characterized by the fact that it can play a dual role in a sentence: 1) form part of the logical "predicate", i.e. represent any property or relation attributed to objects, as in the example “This table is round»; in this function, the universals will be called predicates; 2) be a logical "subject", i.e. represent in the statement an arbitrarily taken object of a certain set of objects of the same type, each of which has a corresponding property, as in the example “Any a crime dangerous to society." Such universals will be called subjects.

The third type of objects are situations (states of things). appropriate situations logical category language expressions are narrative sentences. For example, the presence of the situation where the Volga flows into the Caspian Sea is reproduced in the sentence "The Volga flows into the Caspian Sea", and the situation when the sum of the angles of a triangle is equal to 180 ° is reproduced in the sentence "The sum of the angles of a triangle is 2d", etc. Situations can be simple or complex, depending on whether the sentences representing them are simple or complex. Examples of complex sentences and, accordingly, situations: “If a number is divisible by 6, then it is divisible by 2”; "Yang and his father were at home at the time."

The meaning of a sentence is a judgment. The difference between a judgment and a sentence (as a sign form of a judgment) can be seen when we compare two sentences that are correct translations from one natural language to another: the sign structures are different, but their meaning is the same. Meaning of the sentence and there is judgment. Since we are talking about the logical analysis of the language, the meaning of the sentence is any one of the abstract objects true or False. Thus, the statement “Volga flows into the Caspian Sea” means true (since this sentence reproduces the situation that takes place in reality), and the statement “Volga flows into the Black Sea” is a lie (because it does not correspond to reality).

Each science has specific terms for it. You can talk about mathematical terms: "number", "geometric figure", "set"; there are physical terms such as "mass", "elementary particle", "electric charge"; in biology, the terms "cell", "organism", "heredity" appear; in medicine - “symptom”, “syndrome”, “disease”; in jurisprudence - "legal norm", "crime", "theft". These expressions make up the category descriptive terms(lat. description - description), each of which is a specific object, some property or a set of objects of the same type, etc. In our analysis, descriptive terms are names and universals.

In the language of any science, in addition to descriptive terms that characterize the objects of its own subject area, expressions are used that are used in all sciences. These include some of the particles and conjunctions, such as "and", "or", "if, then", "it is not true that", "then, and only then". With the help of these terms, complex (compound) statements are formed from simple statements (judgments). The same group of “interdisciplinary” terms includes the expressions “is” (“essence”), “all” (“each”), “some” (“exist”), “not”, with the help of which simple singular and plural are built ( general and particular) judgments. They constitute a category logical terms(logical constants).

Without logical terms no judgment can be expressed. They define them to the extreme overall structurelogical form, associated with them logical relations and the laws of logic. Some of these terms are sometimes omitted for the sake of brevity, as in "Man is mortal". In the logical analysis of judgments, we are obliged to restore all these "omissions", which allows us to clarify their logical content, to resolve the issue of their truth or falsity. In particular, the statement just quoted will take the following form: "All people are mortal." And although after such reconstruction and completion these sentences sometimes become somewhat awkward, the thoughts expressed by them acquire clarity and certainty.