212
*dst = *src;
will cause a read of the volatile object pointed to by
pointed to by
. There is no guarantee that these reads and writes are atomic, especially for objects
dst
larger than
.
int
Less obvious expressions are where something which looks like an access is used in a void context.
An example would be,
volatile int *src =
*src;
With C, such expressions are rvalues, and as rvalues cause a read of the object, GCC interprets this as a
read of the volatile being pointed to. The C++ standard specifies that such expressions do not undergo
lvalue to rvalue conversion, and that the type of the dereferenced object may be incomplete. The C++
standard does not specify explicitly that it is this lvalue to rvalue conversion which is responsible
for causing an access. However, there is reason to believe that it is, because otherwise certain simple
expressions become undefined. However, because it would surprise most programmers, G++ treats
dereferencing a pointer to volatile object of complete type in a void context as a read of the object.
When the object has incomplete type, G++ issues a warning.
struct S;
struct T {int m;};
volatile S *ptr1 =
volatile T *ptr2 =
*ptr1;
*ptr2;
In this example, a warning is issued for
you wish to force an error on the first case, you must force a conversion to rvalue with, for instance a
static cast,
static_cast S (*ptr1)
When using a reference to volatile, G++ does not treat equivalent expressions as accesses to volatiles,
but instead issues a warning that no volatile is accessed. The rationale for this is that otherwise it
becomes difficult to determine where volatile access occur, and not possible to ignore the return value
from functions returning volatile references. Again, if you wish to force a read, cast the reference to
an rvalue.
7.3. Restricting Pointer Aliasing
As with gcc, g++ understands the C99 feature of restricted pointers, specified with the
, or
__restrict__
__restrict
language flag,
-std=c99
In addition to allowing restricted pointers, you can specify restricted references, which indicate that
the reference is not aliased in the local context.
void fn (int *__restrict__ rptr, int &__restrict__ rref)
{
/* ... */
}
In the body of
,
fn
rptr
integer.
You may also specify whether a member function's
as a member function qualifier.
__restrict__
void T::fn () __restrict__
;
somevalue
;
somevalue
;
somevalue
*ptr1
.
type qualifier. Because you cannot compile C++ by specifying the
is not a keyword in C++.
restrict
points to an unaliased integer and
Chapter 7. Extensions to the C++ Language
and stores the value into the volatile object
src
, and
causes a read of the object pointed to. If
*ptr2
rref
this
refers to a (different) unaliased
pointer is unaliased by using
Need help?
Do you have a question about the ENTERPRISE LINUX 3 - USING GCC and is the answer not in the manual?
Questions and answers