]>
Commit | Line | Data |
---|---|---|
c63539ff ML |
1 | .. |
2 | Copyright 1988-2022 Free Software Foundation, Inc. | |
3 | This is part of the GCC manual. | |
4 | For copying conditions, see the copyright.rst file. | |
5 | ||
6 | .. index:: accessing volatiles, volatile read, volatile write, volatile access | |
7 | ||
8 | .. _c++-volatiles: | |
9 | ||
10 | When is a Volatile C++ Object Accessed? | |
11 | *************************************** | |
12 | ||
13 | The C++ standard differs from the C standard in its treatment of | |
14 | volatile objects. It fails to specify what constitutes a volatile | |
15 | access, except to say that C++ should behave in a similar manner to C | |
16 | with respect to volatiles, where possible. However, the different | |
17 | lvalueness of expressions between C and C++ complicate the behavior. | |
18 | G++ behaves the same as GCC for volatile access, See :ref:`c-extensions`, for a description of GCC's behavior. | |
19 | ||
20 | The C and C++ language specifications differ when an object is | |
21 | accessed in a void context: | |
22 | ||
23 | .. code-block:: c++ | |
24 | ||
25 | volatile int *src = somevalue; | |
26 | *src; | |
27 | ||
28 | The C++ standard specifies that such expressions do not undergo lvalue | |
29 | to rvalue conversion, and that the type of the dereferenced object may | |
30 | be incomplete. The C++ standard does not specify explicitly that it | |
31 | is lvalue to rvalue conversion that is responsible for causing an | |
32 | access. There is reason to believe that it is, because otherwise | |
33 | certain simple expressions become undefined. However, because it | |
34 | would surprise most programmers, G++ treats dereferencing a pointer to | |
35 | volatile object of complete type as GCC would do for an equivalent | |
36 | type in C. When the object has incomplete type, G++ issues a | |
37 | warning; if you wish to force an error, you must force a conversion to | |
38 | rvalue with, for instance, a static cast. | |
39 | ||
40 | When using a reference to volatile, G++ does not treat equivalent | |
41 | expressions as accesses to volatiles, but instead issues a warning that | |
42 | no volatile is accessed. The rationale for this is that otherwise it | |
43 | becomes difficult to determine where volatile access occur, and not | |
44 | possible to ignore the return value from functions returning volatile | |
45 | references. Again, if you wish to force a read, cast the reference to | |
46 | an rvalue. | |
47 | ||
48 | G++ implements the same behavior as GCC does when assigning to a | |
49 | volatile object---there is no reread of the assigned-to object, the | |
50 | assigned rvalue is reused. Note that in C++ assignment expressions | |
51 | are lvalues, and if used as an lvalue, the volatile object is | |
52 | referred to. For instance, :samp:`{vref}` refers to :samp:`{vobj}`, as | |
53 | expected, in the following example: | |
54 | ||
55 | .. code-block:: c++ | |
56 | ||
57 | volatile int vobj; | |
3ed1b4ce | 58 | volatile int &vref = vobj = something; |