how to make a copy of boost::filesystem::directory_iterator?

主宰稳场 提交于 2019-12-04 03:28:25

问题


I know this sounds stupid, but look at this simple example (working dir should have more than one item):

#define BOOST_FILESYSTEM_VERSION 3
#include <boost/filesystem.hpp>
#include <cassert>

int main()
{
    using namespace boost::filesystem;
    directory_iterator it("./");
    directory_iterator it_copy = it;
    ++it;
    assert(it_copy != it);
    return 0;
}

it_copy is modified together with it! (boost 1.45) What considerations could lead to such design (directory_iterator is something like smart ptr)?

I just need to save a copy of directory_iterator to use it later.


回答1:


If you take a look at the reference you will notice that it is advertised to be a boost::single_pass_traversal_tag.

This is the equivalent (in boost terminology) of the Input Iterator in the STL (think of it as an iterator delivering packets from a network connection, you cannot rewind).

Also note (from this same page):

i == j does not imply that ++i == ++j.

At this point, one may wonder why it can be copied. The reason is that STL algorithms have set the norm taking their arguments by copy. Therefore it would not be usable with STL algorithms if it could not be copied.



来源:https://stackoverflow.com/questions/5381309/how-to-make-a-copy-of-boostfilesystemdirectory-iterator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!