Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 374
qdii Avatar asked Oct 11 '12 13:10

qdii


2 Answers

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.

like image 130
Kerrek SB Avatar answered Nov 12 '22 10:11

Kerrek SB


Presumably since it starts with __ __restrict is an implementation-specific extension that can behave as each implementation desires. I imagine both compilers are correct in this case.

Instead of changing your references to pointers, why not avoid restrict entirely, instead using a profiler to find your hot spots, and only if it shows that such aliasing not covered by the C++ strict-alias rules is taking significant CPU time would I consider changing one specific reference to a pointer.

like image 26
Mark B Avatar answered Nov 12 '22 12:11

Mark B