作者:宝宝抱抱你啊 | 来源:互联网 | 2023-09-24 06:03
from:WhatdoestherestrictkeywordmeaninC?-StackOverflowIwasalwaysunsure,whatdoestherestrict
from : What does the restrict keyword mean in C++? - Stack Overflow
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?
===> a pointer with "restrict" keyword or the implementation/compiler specific version of it, means that the content refered to by the pointer can and only can be accessed through the said pointer.
c++restrict-qualifier
Follow
(Most Voted Answer)
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.
Edit
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 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
Edit2
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.
The Memory Optimisation paper link is dead, here's a link to the audio from his GDC presentation. gdcvault.com/play/1022689/Memory
– Grimeh
Dec 8 '16 at 20:21