Search code examples
c++visual-c++gccreferencerestrict-qualifier

Should I use __restrict on references?


In the program I am coding, one of my function declarations goes like this:

bool parse( const sentence & __restrict sentence )
{
  // whatever
}

When I compile the code with Microsoft Visual Studio 2010 Express, the compiler complains:

warning C4227: anachronism used : qualifiers on reference are ignored

However, this page of GCC’s documentation says:

In addition to allowing restricted pointers, you can specify restricted references, which indicate that the reference is not aliased in the local context.

And the same page gives a very explicit example:

 void fn (int *__restrict__ rptr, int &__restrict__ rref)
 {
   /* ... */
 }

Did I misunderstand MVSC’s warning? or should I transform all my references into pointers so that __restrict applies?


Solution

  • C++ has no notion of restrict in the way C99 does.

    However, several compiler vendors offer extensions to their C++ compilers, which they call __restrict (note the reserved name!). Given that those are extensions, their behaviour is determined by the com­pi­ler vendor. You will have to read the documentation and find out what this extension does in each com­pi­ler separately.

    Just because two vendors chose the same name doesn't mean the extensions have anything in common.