Go to main content
Oracle® Developer Studio 12.5: C++ User's Guide

Exit Print View

Updated: July 2016
 
 

4.14 restrict keyword

C99 introduced the keyword restrict as a type qualifier for function parameters that are pointers. The qualifier means that all accesses to the object pointed to by the argument are made through that pointer. The compiler can make optimizations based on the knowledge that the object is modified only by code that dereferences that pointer.

This C++ language extension is based on the gcc extension described at https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/Restricted-Pointers.html. The keywords __restrict and __restrict__ (but not restrict) are recognized as type qualifiers for function parameters that have pointer or reference type. The qualifier can also be used on a non-static class member function to indicate that the class object is not accessed other than by the "this" pointer.

Any number of function parameters can be declared restricted.

The keywords are recognized in all compiler modes. Use of these keywords other than as described here are errors.

Examples:

int f1(int* __restrict p1, int* __restrict p2);
struct S {
    int f2() __restrict;
};

In the first example, the programmer promises that p1 and p2 point to distinct objects that are not referenced by the function in any other way. In the second example, the "this" pointer is declared restricted, and the programmer promises that the object associated with the function is not accessed other than with "this".

If the program does not in fact honor the promises implied by the __restrict qualifier, the program results are unpredictable.

The –xrestrict command-line option is still honored. Using the __restrict keyword in source code is more flexible, and may be preferred to the command-line option.

See also: –xrestrict[=f]