Should I use __restrict on references?

十年热恋 提交于 2019-12-08 19:00:03

问题


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 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.




回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!