Originally posted by mrugiero
View Post
What I wanted to mean, in a wrong wording, is that people that try to write a fast enough code and look for what language offers for performance, they will put "restrict" everywhere in C, and "const" everywhere in C++.
As restrict is more or less a compiler optimizer hint, about the semantic of pointers that you say you will not break the pointer data by accessing and modifying through other pointer, will reduce (sometimes by a lot) the "load/store" memory accesses.
In comparison the const keyword, if used extensively it doesn't give only a compiler hint for optimizer (which it does), but it also offers code guarantees that no weird stuff happen with your array or whatever you send it by reference (like const reference). This is very useful for common subexpression elimination part, for example in the code:
Code:
auto x = data->getX(); x = x+2; x = data->getX();
In a way is like for simple operations are macros in C vs templates in C++, first is "unchecked" and you do manually write the expansion, the templates are ugly maybe to work with (up-to-the point you get them working) but they are compiler checked.
Being said that, I agree that I wwas not clear about the difference of const and restrict.
Leave a comment: