Take the 2-minute tour ×

I was always unsure, what does the restrict keyword mean in C++?

Does it mean the two or more pointer given to the function does not overlap? What else does it mean?

share|improve this question
restrict is a c99 keyword. Yes, Rpbert S. Barnes, I know that most compilers support __restrict__. You will note that anything with double underscores is, by definition, implementation specific and thus NOT C++, but a compiler specific version of it. –  KitsuneYMG Jan 6 '10 at 9:31
What? Just because it's implementation specific does not make it not C++; the C++ allows for implementation specific stuff explicitly, and does not disallow it or render it not C++. –  Alice May 29 at 5:02

5 Answers 5

up vote 78 down vote accepted

In his paper, Memory Optimization, Christer Ericson says that while restrict is not part of the C++ standard yet, that it is supported by many compilers and he recommends it's usage when available:

restrict keyword

! New to 1999 ANSI/ISO C standard

! Not in C++ standard yet, but supported by many C++ compilers

! A hint only, so may do nothing and still be conforming

A restrict-qualified pointer (or reference)...

! ...is basically a promise to the compiler that for the scope of the pointer, the target of the pointer will only be accessed through that pointer (and pointers copied from it).

In C++ compilers that support it it should probably behave the same as in C.

See this SO post for details: Realistic usage of the C99 ‘restrict’ keyword?

Take half an hour to skim through Ericson's paper, it's interesting and worth the time.


I also found that IBM's AIX C/C++ compiler supports the __restrict__ keyword.

g++ also seems to support this as the following program compiles cleanly on g++:

#include <stdio.h>

int foo(int * __restrict__ a, int * __restrict__ b) {
    return *a + *b;

int main(void) {
    int a = 1, b = 1, c;

    c = foo(&a, &b);

    printf("c == %d\n", c);

    return 0;

I also found a nice article on the use of restrict:

Demystifying The Restrict Keyword


I ran across an article which specifically discusses the use of restrict in C++ programs:

Load-hit-stores and the __restrict keyword

Also, Microsoft Visual C++ also supports the __restrict keyword.

share|improve this answer

Nothing. It was added to the C99 standard.

share|improve this answer
That's not completely true. Apparently it's supported by some C++ compilers and some people strongly recommend it's usage when it's available, see my answer below. –  Robert S. Barnes Dec 27 '09 at 8:23
@Robert S Barnes: The C++ standard does not recognize restrict as a keyword. Hence my answer stands correct. What you describe is implementation specific behavior and something that you should not really rely on. –  dirkgently Dec 27 '09 at 14:00
@dirkgently: With all due respect, why not? Many projects are tied to specific non-standard language extensions supported by only specific or very few compilers. The Linux Kernel and gcc comes to mind. It's not uncommon to stick with a specific compiler, or even a specific revision of a specific compiler for the entire useful lifetime of a project. Not every program needs to be strictly conforming. –  Robert S. Barnes Dec 27 '09 at 19:19
@Rpbert S. Barnes: The question said c++. Not MSVC, not gcc, not AIX. If acidzombie24 wanted compiler specific extensions, s?he should have said/tagged so. –  KitsuneYMG Jan 6 '10 at 9:29
@dirkgently Steam. –  Crashworks Nov 1 '12 at 8:22

This is the original proposal to add this keyword. As dirkgently pointed out though, this is a C99 feature, it has nothing to do with C++.

share|improve this answer
Many C++ compilers support the __restrict__ keyword which is identical as far as I can tell. –  Robert S. Barnes Dec 27 '09 at 12:45

There's no such keyword in C++. List of C++ keywords can be found in section 2.11/1 of C++ language standard. restrict is a keyword in C99 version of C language, but in in C++.

share|improve this answer
@Robert: But there is no such keyword in C++. What individual compilers do is their own business, but it is not part of the C++ language. –  jalf Dec 27 '09 at 16:02

Since header files from some C libraries use the keyword, the C++ language will have to do something about it.. at the minimum, ignoring the keyword, so we don't have to #define the keyword to a blank macro to suppress the keyword.

share|improve this answer
I would guess that's either handled by using an extern C declaration, or by it being silently dropped, as is the case with the AIX C/C++ compiler, which instead handles the __rerstrict__ keyword. That keyword is also supported under gcc so that code will compile the same under g++. –  Robert S. Barnes Dec 27 '09 at 12:44

Your Answer


