In boost directory_iterator example - how to list directory files not recursive (see this answer) is the sample code
#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>
...
using namespace boost::filesystem;
for(auto& entry : boost::make_iterator_range(directory_iterator(p), {}))
{
std::cout << entry << "\n";
}
(p
is of type boost::filesystem::path
.)
Upon looking at the documentation for make_iterator_range
, I think the constructor being called is this one:
template< class ForwardTraversalIterator >
iterator_range< ForwardTraversalIterator >
make_iterator_range( ForwardTraversalIterator Begin,
ForwardTraversalIterator End );
If I'm right, then the second argument passed in the code example above, {}
, seems to correspond to the end of whatever container is invisibly iterated over by the directory_iterator
.
I've never seen this before.
Is it possible to construct the end
iterator just by value-constructing such an iterator from the empty initializer list {}
? (Am I even phrasing this properly?)
I wouldn't mind having someone spell out what is going on under the hood, given that the type of the iterator so constructed must match the type of the first iterator (directory_iterator(p)
). (Is template argument deduction going on here?)
Yes, it's valid.
There's no template argument deduction beyond that which you already usually invoke: your first argument is of type directory_iterator
, so the function is instantiated as such.
Following on from that, templates aside, now you are calling a function that takes two directory_iterators
: the {}
can only initialise a directory_iterator
at that point, as that's what your function [template instance] takes. Hence, in this case, writing {}
is functionally equivalent to writing directory_iterator{}
.
If the directory_iterator
could not be constructed from {}
, your program would not compile.