how do I determine the type when auto was used

closed account (2z0kLyTq)
In the example code below auto was used I want to know the type to replace auto in this code. typeid gave me this: "NSt10filesystem7__cxx1115directory_entryE".

And gdb told me this: "const std::filesystem::__cxx11::directory_entry &"

So I assumed "auto" could be replace with "std::filesystem::directory_entry", but I was wrong.

I don't just want to know the type but a reasonable method for determining the type.


1
2
 for(auto& dit: std::filesystem::directory_iterator(".")){
         std::cout << typeid(dit.name() << '\n';


Thanks
Last edited on
typeid gave me this: "NSt10filesystem7__cxx1115directory_entryE".

Actually your code, even adding the missing parentheses, does not compile:
main.cpp:
1
2
3
4
5
6
7
8
9
10
11
#include <filesystem>
#include <iostream>
#include <typeinfo>


int main()
{
    for (auto& dit : std::filesystem::directory_iterator(".")) {
        std::cout << typeid(dit.name()) << '\n';
    }
}

Output:
main.cpp: In function 'int main()':
main.cpp:9:33: error: 'const class std::filesystem::__cxx11::directory_entry' has no member named 'name'
    9 |         std::cout << typeid(dit.name()) << '\n';
      |                                 ^~~~


Assuming you meant:
1
2
3
4
5
6
7
8
9
10
11
#include <filesystem>
#include <iostream>
#include <typeinfo>


int main()
{
    for (auto& dit : std::filesystem::directory_iterator(".")) {
        std::cout << typeid(dit).name() << '\n';
    }
}

since you’re tacking advantage of a directory_iterator, and directory_iterators
https://en.cppreference.com/w/cpp/filesystem/directory_iterator
cppreference wrote:
directory_iterator is a LegacyInputIterator that iterates over the directory_entry elements of a directory

‘dit’ is likely to be a ‘const std::filesystem::directory_entry&’.

Let’s try:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <filesystem>
#include <iostream>
#include <typeinfo>


int main()
{
    for ( const std::filesystem::directory_entry& dit
          : std::filesystem::directory_iterator(".") )
    {
        std::cout << typeid(dit).name() << '\n';
    }
}


Bingo.
Last edited on
closed account (2z0kLyTq)
Thank you. I think I let myself get frustrated. When I reviewed the gdb output it clearly said:
"const std::filesystem::__cxx11::directory_entry&".

I was foolishly leaving the const off.

I did go to cppreference before posting, but I couldn't figure out how "LegacyInputIterator" translates to "const std::filesystem::directory_entry"

Now that I have the answer I can see it was documented.

Again thank you for taking the time to respond.
Last edited on
I was foolishly leaving the const off.


And the ref ( & ) .
Repeater wrote:
And the ref ( & ) .

+1 Repeater.
I think it was the point, too.

- - -
To be honest, wilson52, and this isn’t a rant, I think your worst mistake was not providing a compilable code.
There’re people here who answer several times every day, and I think they’re sick and tired of correcting the codes just to get the compiler directions, so they sometimes neglect some post.
If you fancy fast and canny answers, take your time to ask good questions.
Just a suggestion :)
Was it me who pushed the OP to delete their account?
I didn’t expect such a reaction. Did I really sound that harsh?
OMG, I’m sorry…
No, you can tell from his response that that is not the case.
Some people just have this weird "leave no trace" mindset after a question has been answered.
Thank you, Ganado.
Topic archived. No new replies allowed.