c++ - Does iterator need to include any type information from the container by the standard? -


today came upon this question , started wonder inconsistencies between gcc/clang , visual studio.

the question general, still i'd understand - standard impose rules on whether iterator type should or should not contain container specific type information. consider snippet:

#include <unordered_map> #include <iostream>  struct hash1 {     size_t operator()(int key) const     {         return key;     } };  struct hash2 {     size_t operator()(int key) const     {         return key + 1;     }  };  int main(int argc, char** argv) {     std::unordered_map<int, int, hash1> map1;     map1[1] = 1;     std::unordered_map<int, int, hash2> map2;     map2[1] = 1;     std::unordered_map<int, int, hash2>::iterator it1 = map1.find(1);     std::unordered_map<int, int, hash2>::iterator it2 = map2.find(1);     if (it1 == it2) // visual studio 2015 gives assertion on iterator type inequality     {         std::cout << "equal";     }     else     {         std::cout << "not equal";     } } 

so in visual studio std::unordered_set iterator type std::list iterator, of course not know hash function type , can perform assignment, equality operator gives assertion since internally compares containers. gcc/clang on other hand ignore type incompatibility there no run time warnings/errors.

what standard on topic?

a pair of iterators describes sequence of values. if try 2 iterators not point @ elements of same sequence behavior of program undefined, i.e., standard doesn't impose requirements on program does.

note previous paragraph doesn't use word "container" anywhere. containers 1 way of creating sequences, not way. so, no, there no requirement iterators know containers, because iterators have no inherent connection containers.


Comments

Popular posts from this blog

How to check data type in C++? -

javascript - Is getTimezoneOffset() stable during daylight saving transition? -

sql server - SQL Query returns one result, does not return 0 or NULL result -