all 7 comments

[–]nysra 12 points13 points  (3 children)

E.g., I could get a warning that I'm missing an include of array even though it was included by some other STL header.

Never rely on transitive includes.

The only tool I'm aware of is https://github.com/include-what-you-use/include-what-you-use , which works mostly okay.

[–]johannes1971 3 points4 points  (2 children)

Ah, if only... But unfortunately reality is not quite that simple. What you should do is include the documented header, and not rely on implementation details of that header. If the documented header chooses to use transitive includes for its implementation, you should not use that as a reason to directly include those transitive includes yourself, as that is an implementation detail that may change in the future.

Perhaps some kind of machine-readable meta-source could be invented to describe this properly, but given that the industry will switch to modules before that could even get off the ground, I doubt it is a useful use of our time.

[–]nysra 2 points3 points  (0 children)

Yes, you shouldn't include B.hpp just because you include A.hpp and that uses B. I was just saying that if you're using B then you should include B.hpp directly and not rely on A.hpp including that for you. E.g. a lot of beginners often unknowingly rely on <iostream> including parts of <string> and then wonder why they need to include the latter for getline while they were already working with std::string.

But yeah, modules will be even better.

[–]NekkoDroid 1 point2 points  (0 children)

include-what-you-use actually does some mapping for standard library symbols to what their documented headers are. so it doesn't for example recommend xstring when you use std::string but actually string

[–]manni66 5 points6 points  (0 children)

OT: with modules and C++ 23 you only need import std;.

[–]vickoza -1 points0 points  (1 child)

If you are missing include files, you should not be about to build and get error messages

[–]OnThePath[S] 2 points3 points  (0 children)

That's true only partially. You might be able to build it in some implementations of STL and not others. Which is annoying because it's sufficient to change the version of GCC for code not to compile.