Re: Deprecate the use of plain pointers as standard container iterators
Nicola Musatti wrote:
Hallo,
I belive that the use of plain pointers as iterator types for standard
library containers should be deprecated, because it limits genericity
and portability.
The following code is valid on some platforms and not on others:
#include <vector>
#include <algorithm>
void f() {
std::vector<int> v;
v.push_back(3);
v.push_back(2);
v.push_back(1);
std::sort(++v.begin(), v.end());
}
If std::vector<>::iterator is a pointer the expression ++v.begin() is
invalid because it attempts to modify an rvalue.
std::sort(boost::next(v.begin()), v.end());
works and is generic (and of course boost::next is
trivial to implement).
The following code gives different results on different platforms:
#include <vector>
#include <algorithm>
#include <iostream>
int * find(int * b, int * e, int d) {
return b + d < e ? b + d : e;
}
int main() {
std::vector<int> v;
v.push_back(3);
v.push_back(2);
v.push_back(1);
std::vector<int>::iterator i = find(v.begin(), v.end(), 2);
std::cout << ( i != v.end() ? *i : 0 ) << '\n';
}
If std::vector<>::iterator is a pointer the user defined find function
is chosen.
What's to say that std::vector<T>::iterator might not be
a UDT defined in a namespace nested within ::std? In that
case your code might also find no "find", or the wrong
"find"? It sounds like you wish to require that the
iterators must be of a type defined in namespace ::std;
the question is whether the benefit is worth the cost,
which might be slower performance on some systems (as
well as possibly encouraging people to write code in a
less robust style by relying on ADL where it's not needed).
-- James
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]