Template iterator confusion

A line like the following appears on en.cppreference.com/w/cpp/experimental/fs/directory_iterator:

 
for(auto& p: fs::directory_iterator("path_to_a_directory"))

1. I thought all for statements had to have two semicolons in them. What's this?

2. What does the colon in that line mean/do?

3. I don't want to use "auto," because I want the code to tell me what the data type is. What is the data type? I can't figure it out.
Last edited on
> I thought all for statements had to have two semicolons in them. What's this?

See: http://www.stroustrup.com/C++11FAQ.html#for


> I don't want to use "auto," because I want the code to tell me what the data type is.

In general the code is more readable (and certainly more maintainable) when we let the compiler deduce the type.
IMHO.


> What is the data type?

std::experimental::filesystem::directory_entry (C++14, Filesystem TS)
std::filesystem::directory_entry (C++17)
http://en.cppreference.com/w/cpp/experimental/fs/directory_entry

1
2
for( const fs::directory_entry& p: fs::directory_iterator("sandbox"))
        std::cout << p << '\n';

I didn't want "auto" because I wanted to understand what was going on, but the Stroustrup link makes it all clear. I hadn't realized the let's-make-C++-a-high-level-language crowd had had so much success.

Thanks!
Last edited on
Topic archived. No new replies allowed.