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