5

In C there is restrict keyword which tells the compiler that there is no aliasing between pointer arguments of a function, allowing it this way to perform some optimizations which otherwise will not be allowed. For example:

void add(int* restrict ptrA,
         int* restrict ptrB,
         int* restrict val)
{
    *ptrA += *val;
    *ptrB += *val;
}

Now the instructions in function body can be executed in parallel because there is not aliasing between val and some of the ptr arguments. In C++ there is no restrict keyword.

  1. What are exact rules for dealing of pointer aliasing in C++ defined by the standard?
  2. What are popular compilers extensions which provide semantic similar to restrict in C? For example for MSVC, g++, clang++ and Intel C++ compiler.
  3. Is there any plans restrict keyword to be standardized in the future C++ standards?
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
bobeff
  • 3,543
  • 3
  • 34
  • 62
  • 1
    related: http://stackoverflow.com/questions/25940978/c-restrict-semantics – NathanOliver Oct 28 '16 at 11:35
  • 1
    See this article: https://en.wikipedia.org/wiki/Restrict - "C++ does not have standard support for restrict, but many compilers have equivalents that usually work in both C++ and C, such as the GNU Compiler Collection's and Clang's __restrict__, and Visual C++'s __restrict and __declspec(restrict)." – Mayhem Oct 28 '16 at 12:39
  • (1) alone is a very large question. – Barry Oct 28 '16 at 14:18
  • 1
    Also see [How to avoid aliasing and improve performance?](https://stackoverflow.com/q/31601142/1708801) – Shafik Yaghmour Jul 07 '18 at 06:52
  • I'd say this question should be split up in order for someone to attempt an answer. I wouldn't mind hearing about where restrict standardization stands, I haven't read about it in years. – einpoklum May 12 '22 at 22:20
  • Potential dupe, in the sense that it gives a partial answer to (1.) and (2.): [What does the `restrict` keyword mean in C++?](https://stackoverflow.com/q/776283/1593077). – einpoklum May 16 '23 at 18:00

0 Answers0