Is there any advantage to specifying the MSVC/GCC non-standard __restrict qualifier on a function pointer parameter if it is the only pointer parameter? For example,
int longCalculation(int a, int* __restrict b)
My guess is it should allow better optimization since it implies b does not point to a, but all examples I’ve seen __restrict two pointers to indicate no aliasing between them.
As mentioned in the comments
bcan’t point toaanyways, so there is no aliasing potential there anyways. So if the function is pure in the sense that it works only on its parameters there shouldn’t be any real benefits.However if the function uses global variables internally then
__restrictmight offer benefits once again, since it makes clear thatbdoesn’t point to any of those global variables.An interesting case might be the situation where you allocate and deallocate memory inside the function. The compiler could theoretically be sure that
bdoesn’t point to that memory, however whether or not it realizes that I’m not sure and might depend how the allocation is called.Personally however I prefer to keep
__restrictout of the signature and do something like thisIMO this has the following advantages:
__restrictused__restrictusingassert, since passing aliasing pointers to a function expecting them to be nonaliasing can lead to hard to track down bugs.