Can we restrict pointer aliasing of vector iterators?

Hi everyone,
in my recent shift from arrays and pointers to vectors and iterators, I have begun to wonder whether I can restrict pointer aliasing of vector iterators. Take this for example:

1
2
3
4
5
6
vector<int>::iterator it1 = vec1.begin();
vector<int>::iterator it2 = vec2.begin();
vector<int>::iterator lim_it1 = vec1.end();
for (; it1 != lim_it1; it1++, it2++) {
  *it2 += *it1;
}


Could I restrict pointer aliasing of the iterators?
¿what do you mean with 'pointer aliasing'?
Well the iterators, which are essentially pointers, could be aliased as they could dereference to the same value. But since this is not happening I'd like to tell the compiler about it, much like you do with the restrict command for pointer declarations...
The restrict keyword is a C language keyword, it does not exist in C++.
sorry, I meant __restrict__. Would really appreciate an answer to my question though; I have looked online but haven't really found an answer.
C++ simply has no concept of restricted pointers or any other extensions of the strict aliasing rule, except that the data in std::valarrays are never aliased.

A few C++ compilers transplanted restricted pointers from C, but it remains a compiler-specific language extension, and it's up to the compiler vendor to consider extending it to std::vector's, std::string's, and std::array's iterators (no other iterators would work even in principle, by the way). In practice, compiler vendors seem to put more effort in automatic vectorization of the loops found in the standard library algorithms instead. So far, to the best of my knowledge, the answer is "no".
Last edited on
Thanks for the explanation :)
C++ has strict aliasing so if you have a pointer or iterator to an int the compiler can assume the data will not be the same as the data pointed to by a float pointer or other types. There are some exception to this rule. void*, char* and unsigned char* (perhaps some more) can point to data of any type.

Topic archived. No new replies allowed.