Can we restrict pointer aliasing of vector iterators?

Dec 14, 2011 at 8:42am
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?
Dec 14, 2011 at 4:59pm
¿what do you mean with 'pointer aliasing'?
Dec 14, 2011 at 10:33pm
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...
Dec 14, 2011 at 10:39pm
The restrict keyword is a C language keyword, it does not exist in C++.
Dec 15, 2011 at 1:42am
sorry, I meant __restrict__. Would really appreciate an answer to my question though; I have looked online but haven't really found an answer.
Dec 15, 2011 at 2:57am
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 Dec 15, 2011 at 2:58am
Dec 15, 2011 at 4:01am
Thanks for the explanation :)
Dec 15, 2011 at 10:51am
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.