c++: create a insert_iterator without #include <iterator>

238 Views Asked by At

In the code below I create an insert_iterator without including the iterator header. How is possible that this compiles? I'm new to c/c++ so this will likely have an obious explanation.

#include <list>
//#include <iterator>

int main()
{

    std::list<int> mylist(10,1);
    std::insert_iterator<std::list<int> > it(mylist,mylist.begin());

    return(0);
}
1

There are 1 best solutions below

1
On

It happens to compile because some of the other headers, <list> in your case, is pulling <iterator> as well. This may work fine with one implementation but fail on a different one, or even on the next version of your current library implementation.

You should always include the headers that define the stuff you use, to make sure it will compile everywhere. Note that there are some guarantees of standard headers that are pulled by other standard headers, but I don't think this is one of those exceptions.