Mutable symmetry with const modifiers

zhaozj2021-02-16  60

Mutable symmetry with const modifiers

Smilemac

I am afraid that there is no programmer to use const on the use of Const, but most of the programmers are very opposed to using Mutable, so I also pay little attention to Mutable and Const symmetry.

We know, Const is a very good modifier word in semantics and grammar. This is actually in C , and C a lot of grammar concept is blurred in semantics, but const is a different number. The Const promise is that once a variable is modified, as long as the forced conversion is not used, the value of the variable will not be changed in any case, whether it is interested or unintentional, and is the same by const modified, once a certain The function is modified by const, then it cannot directly or indirectly change the value of variables other than any function body, even if it is called a function that may cause this change. This commitment also makes strict guarantees in grammar, and any behavior that may violate this commitment will be checked out by the compiler.

Using const, we can declare a part of the invariant's nature, there are many examples, this article is not described, this article focuses on the following mutable.

Mutable is not as good as constant, just like const, Mutable is one of C some of the modified words with clear semantics.

Mutable commitment is if a variable is modified, then this variable will always be a variable state, even in a const function. This has a symmetrical definition with const, and one will never change, and the other is always changeable.

Some sayings are only those internal variables that do not change the external behavior characteristics of the object, can be declared as mutable, in fact, not completely.

This world has something eternal, but some things are always in changing, even if you declare all other things related to eternal, it still changes, because once it becomes eternal, then The things it represents have lost their meaning, which is the meaning of Mutable.

A ready-made example is Scopeguard, reading its implementation readers know that it uses Mutable in a primary class ScopeGuardImplBase [2]:

Class ScopeguardImplbase {public: void dismiss () const throw () {DISMISSED_ = true;

protected:

ScopeGuardImplbase (): DISMISSED_ (FALSE)

{} ScopeGuardImplbase (Const ScopeguardImplbase & Other)

: dismissed_ (other.dismissed_)

{Other.dismiss ();} ~ scopeguardimplbase () {}

Mutable Bool Dismble_; private:

// disable assistment

ScopeguardImplbase & operator =

Const ScopeguardImplbase &);

}

In this example, we can see that the status variable dismissed is declared as Mutable, our aunt, whether or not the original author is only used as a skill, the purpose is to bypass the restrictions of the provision of temporary variables, we only In view of semantics, the answer is obvious, regardless of any case, dismissed should be variable, otherwise the entire ScopeGuard has lost its original interest. It represents such a specification: this type (referring to scopeguard) variable cannot be declared as const, (even if you do this, it is also invalid). Of course, the same specifications can also be made by const, such as if the function dismiss () is not declared as const, but the problem is that you cannot assume the user's use environment, because ScopeGuard definition For it to execute any specified actions while leaving the defined interval, there is no constraint for the action itself, so you must assume that the user may also pass the reference to a certain ScopeGuard object into a const function (so It is useful in some cases). Under this condition, mutable is necessary.

Such an example can also mention some, such as a log object, a log object of a constant, and its storage variable must be Mutable. For example, an access counter, some of the Dirty flags of some special objects, and more.

From the perspective of language, there must be a completely opposite modification of Mutable, otherwise it will not be a complete division.

In fact, it is difficult to find such a good modifier word, you see if a variable or function should be constant, just look at whether it should be constant or invariant in your design space, and see if a variable should Is Mutable, just look at whether it is in your design space is Forever Mutative.

This opposite and symmetrical properties, the two intersections are empty, so that Mutable can completely negate the constraints made by Const. Both combination, you can give an object more specifications. And this symmetry, careful taste, you will find that its meaning is endless, with strong aesthetic properties.

Reference: 1. ISO / IEC 14882, "Programming Languages-C , 1998 2. Andrei Alexandrescu and Petru Marginean," Change the Way You Write Exception-Safe Code - Forever ", CUJ, DEC 2002

转载请注明原文地址:https://www.9cbs.com/read-25232.html

New Post(0)