问题
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?
回答1:
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 compiler vendor. You will have to read the documentation and find out what this extension does in each compiler separately.
Just because two vendors chose the same name doesn't mean the extensions have anything in common.
回答2:
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.
来源:https://stackoverflow.com/questions/12841279/should-i-use-restrict-on-references