restrict

Jan 16, 2010 at 7:19am
The restrict keyword in not standard in C++. In g++, __restrict or __restrict__ can be used. But I don't understand which. I found code examples using __restrict and others using __restrict__. Which one should I use in my C++ code? And if I want some of my code to be C-compatible, should I use a #define which will allow me two switch to restrict if C-compatibility is enabled?
Jan 16, 2010 at 10:17am
I still don't fully understand the rules for restrict. Is it just a C thing and not a C++ thing? Do compilers even take advantage of it?

Anyway, due to the uncertainties you mentioned, I would probably go the define route:

1
2
3
#define RESTRICT __restrict__

void MyFunc(RESTRICT int* foo) { }


This way if there's a problem with __restrict__ or restrict or __restrict, or whatever, you can just change the #define to get it to compile, rather than ransack your code.

On the downside, it's ugly.
Last edited on Jan 16, 2010 at 10:18am
Jan 16, 2010 at 11:25am
Actually it doesn't have to be ugly: replace RESTRICT with restrict. The question is whether __restrict or __restrict__ should be used in g++.

(restrict IS a "C thing" and is non-standard in C++, but since it's as beneficial in C++ as it is in C, I don't see why not use it in C++ code too)

(By the way, RESTRICT int* foo should be int *RESTRICT foo. If I'm not mistaken)

And yes, compilers do take advantage of it. If I'm not mistaken (maybe I am!), restric is less useful with x86 processors than with Sun's and Apple's ones (in terms of performance gain), but its use whenever possible is still reccomended.
Last edited on Jan 16, 2010 at 11:28am
Jan 16, 2010 at 5:18pm
It justs exists to assist with alias optimisation issues. It's not a big deal, other than to stop you making up aliases. C compilers have coped with it for 20+ years now without it.
Last edited on Jan 16, 2010 at 5:19pm
Jan 17, 2010 at 5:33pm
I know what restrict does...I'd just like to know whether I should use __restrict or __restrict__ in my C++ code when I compile it with g++.
Jan 17, 2010 at 11:11pm
Topic archived. No new replies allowed.