Can volatile be used wherever const is used?

I wonder if volatile can be used somewhere const , and what every case would mean.

 volatile dummy_class volatile dummy_class& dummy_class volatile* dummy_class *volatile dummy_class volatile *volatile 

These are all the different cases where const involved, do the same semantics apply for volatile ?

+4
source share
4 answers

This is section 3.9.3 CV qualifiers from C ++ 11 draft n3290:

The type referred to in paragraphs 3.9.1 and 3.9.2 is categorically unqualified. Each type, which is a cv-unqualified complete or incomplete object type or is invalid (3.9), has three corresponding versions of the cv-qualification of its type : a version with a configuration-independent version, a version with variable qualifications and a version with stable volatility. The term type (1.8) object includes the cv qualifiers specified when creating the object. The presence of the const qualifier in the declaration-specifier-seq declares an object of the type of the object corresponding to const; such an object is called a const object. The presence of a volatile qualifier in the qualifier declaration-seq declares an object of the type of a volatile-qualified object; such an object is called a mutable object. The presence of both cv-qualifiers in decl-specifier-seq declares an object type object with stable power consumption; such an object is called a const volatile object. Skilled or cv-unskilled type versions are different types; however, they must have the same understanding and alignment requirements (3.9) .51

So const and volatile can be used in the same places, possibly in conjunction.

In paragraph 3 of this section, there is a slight difference in how they apply to class objects:

Each non-static, non-volatile, non-referenced data element of an object with a const-const class is const-qualified, each non-static, non-referenced data element of an object with a variable class is unstable and is similar for members of an unstable class. See 8.3.5 and 9.3.2 for types of functions that have CV classifiers.

but this is pretty logical.

volatile -qualified objects have more stringent requirements for the as-if rule, namely:

Access to unstable objects is evaluated strictly in accordance with the rules of an abstract machine.

Volatility is attached to an object in the same way const does:

 dummy_class volatile* // non-volatile pointer to volatile object dummy_class *volatile // volatile pointer to non-volatile object dummy_class volatile *volatile // volatile pointer to volatile object 

For non-static member functions (Β§9.3.1):

A non-static member function can be declared const, volatile or const volatile. These cv qualifiers affect the type of this pointer (9.3.2). They also affect the type of function (8.3.5) of a member function; a member function declared const is a constant member function, a member function declared volatile is a volatile member function and a member function declared volatile is a constant mutable function member.

Thus, volatility, such as const-ness, is applied to the this type inside the function.

None of const and volatile can be applied to static member functions.

+2
source

Nearly. Both are cv qualifiers and can be used almost everywhere else can be. The only place they appear in C ++ grammar is here:

CVs:
const
volatile

And the rest of the grammar refers to them as a cv classifier.

Please note that in one place where you did not indicate where cv qualifiers can be used, follow these steps.

 struct X { void f() const; void g() volatile; void h() const volatile; }; 

See http://drdobbs.com/cpp/184403766 for later use.

However, there is a rule in which you cannot combine const and mutable , as in:

 stuct X { mutable const int x; // error mutable volatile int y; // valid }; 

The relevant place in the standard is [dcl.stc]:

A mutable qualifier can only be applied to the names of data members of the class (9.2) and cannot be applied to declared declared constants or static [...]

+6
source

Any variable can be declared const or volatile (or both), but they have different semantics.

The const keyword indicates that the variable is constant. The volatile keyword indicates that a variable can have its own content, even if you don't assign anything to it.

For example, if you have this piece of code

 int a = ...; int b = ...; int sum1 = (a + b) * 2; int sum2 = (a + b) + 16; 

and enable optimization in your compiler, the compiler can optimize the previous code:

 int a = ...; int b = ...; int temp = a + b; int sum1 = temp * 2; int sum2 = temp + 16; 

This optimization occurs because the compiler assumes that the values ​​of a and b will not change when calculating sum1 and sum2, so a + b will not change. If you declare a or b as volatile , this assumption is denied, which means that the compiler will generate code that calculates a + b each time and does not save it in a temporary place. This is because declaring a or b volatile indicates that a or b (and therefore a + b) can be changed between two statements.

Although volatile looks like a useless keyword, it has its uses in multi-threaded applications and when performing memory I / O (using system memory to communicate with hardware). For example, in GameBoy Advance you need to read a specific memory location to determine which keys are pressed at the same time. If you indicate a pointer to this specific memory location as volatile, it will always be guaranteed that you will read this data from memory each time.

+1
source

In C ++ 2003, I think that you cannot define a member of the static int volatile class until you can do this with the static int const member: the latter becomes a constant expression and until you need an address or an object reference, you will be fine. For C ++ 2011, I don’t know if this has changed, because you can initialize the members in the class definition, and I don’t know if this applies to static members.

+1
source

Source: https://habr.com/ru/post/1392195/


All Articles