I have some code that I’m updating to C++11 using gcc 4.7 (from 3.1)
I have a multiset defined as a private member of a class:
multiset <Object*, objectcomp> objects_;
In the code is a segment that looks like this (p_q is a pair of multiset iterators, sorry about that nasty line, can’t wait to replace that with auto, haha):
void Terrain::removeObject(Object* obj){
pair<multiset<Object*, objectcomp>::iterator, multiset<Object*, objectcomp>::iterator> p_q;
multiset<Object*, objectcomp>::iterator p,q;
q = NULL;
p_q = objects_.equal_range(obj);
for(p = p_q.first; p != p_q.second; p++){
if(*p == obj) {q=p; break;}
}
if(q!=NULL){
... do stuff based on q no longer being null
}
}
This won’t compile anymore. Can you not set iterators to null anymore? What is the alternative? (nullptr doesn’t work either)
It was never legal to set iterators to NULL. You may have gotten lucky because your particular implementation happened to use pointers as iterators for that type, but it was still illegal.
The right answer is:
Or, in some cases: