Extensions To The C++ Language; Minimum And Maximum Operators In C; When Is A Volatile Object Accessed - Red Hat ENTERPRISE LINUX 4 Manual

Hide thumbs Also See for ENTERPRISE LINUX 4:
Table of Contents

Advertisement

The GNU compiler provides these extensions to the C++ language (and you can also use most of the
C language extensions in your C++ programs). If you want to write code that checks whether these
features are available, you can test for the GNU compiler the same way as for C programs: check for
a predefined macro

7.1. Minimum and Maximum Operators in C++

It is very convenient to have operators which return the "minimum" or the "maximum" of two argu-
ments. In GNU C++ (but not in GNU C),
?
a
b
is the minimum, returning the smaller of the numeric values
?
a
b
is the maximum, returning the larger of the numeric values
These operations are not primitive in ordinary C++, since you can use a macro to return the minimum
of two things in C++, as in the following example.
#define MIN(X,Y) ((X)
You might then use
.
j
However, side effects in
fail, incrementing the smaller counter twice. The GNU C
macros that avoid this kind of problem (Section 6.6 Referring to a Type with
ing
and
as macros also forces you to use function-call notation for a fundamental arithmetic
MIN
MAX
operation. Using GNU C++ extensions, you can write
Since
and
are built into the compiler, they properly handle expressions with side-effects;
?
?
min = i++
? j++;

7.2. When is a Volatile Object Accessed?

Both the C and C++ standard have the concept of volatile objects. These are normally accessed by
pointers and used for accessing hardware. The standards encourage compilers to refrain from opti-
mizations concerning accesses to volatile objects that it might perform on non-volatile objects. The C
standard leaves it implementation defined as to what constitutes a volatile access. The C++ standard
omits to specify this, except to say that C++ should behave in a similar manner to C with respect
to volatiles, where possible. The minimum either standard specifies is that at a sequence point all
previous accesses to volatile objects have stabilized and no subsequent accesses have occurred. Thus
an implementation is free to reorder and combine volatile accesses which occur between sequence
points, but cannot do so for accesses across a sequence point. The use of volatiles does not allow you
to violate the restriction on updating objects multiple times within a sequence point.
In most expressions, it is intuitively obvious what is a read and what is a write. For instance

Extensions to the C++ Language

. You can also use
__GNUC__
(Y) ? : (X) : (Y))
int min = MIN (i, j);
or
may cause unintended behavior. For example,
X
Y
works correctly.
to test specifically for GNU C++ ().
__GNUG__
and
a
and
a
to set
to the minimum value of variables
min
extension allows you to write safe
typeof
int min = i
Chapter 7.
;
b
.
b
MIN (i++, j++)
). However, writ-
typeof
instead.
? j;
and
i
will
int

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the ENTERPRISE LINUX 4 and is the answer not in the manual?

Table of Contents