]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/doc/gcc/extensions-to-the-c++-language/restricting-pointer-aliasing.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / gcc / doc / gcc / extensions-to-the-c++-language / restricting-pointer-aliasing.rst
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:: restricted pointers, restricted references, restricted this pointer
7
8 .. _restricted-pointers:
9
10 Restricting Pointer Aliasing
11 ****************************
12
13 As with the C front end, G++ understands the C99 feature of restricted pointers,
14 specified with the ``__restrict__``, or ``__restrict`` type
15 qualifier. Because you cannot compile C++ by specifying the :option:`-std=c99`
16 language flag, ``restrict`` is not a keyword in C++.
17
18 In addition to allowing restricted pointers, you can specify restricted
19 references, which indicate that the reference is not aliased in the local
20 context.
21
22 .. code-block:: c++
23
24 void fn (int *__restrict__ rptr, int &__restrict__ rref)
25 {
26 /* ... */
27 }
28
29 In the body of ``fn``, :samp:`{rptr}` points to an unaliased integer and
30 :samp:`{rref}` refers to a (different) unaliased integer.
31
32 You may also specify whether a member function's :samp:`{this}` pointer is
33 unaliased by using ``__restrict__`` as a member function qualifier.
34
35 .. code-block:: c++
36
37 void T::fn () __restrict__
38 {
39 /* ... */
40 }
41
42 Within the body of ``T::fn``, :samp:`{this}` has the effective
43 definition ``T *__restrict__ const this``. Notice that the
44 interpretation of a ``__restrict__`` member function qualifier is
45 different to that of ``const`` or ``volatile`` qualifier, in that it
46 is applied to the pointer rather than the object. This is consistent with
47 other compilers that implement restricted pointers.
48
49 As with all outermost parameter qualifiers, ``__restrict__`` is
50 ignored in function definition matching. This means you only need to
51 specify ``__restrict__`` in a function definition, rather than
52 in a function prototype as well.