Can this be an exception to the rule about not including header files in header files? Follow Post Reply. Keith Thompson. Where did you get the idea that there's such a rule? There's nothing wrong with including header files in header files. Do it if you need to, don't do it if you don't. Most or all? This is something. Therefore, we must do this. Richard Heathfield. Keith Thompson said: There's nothing wrong with including header files in header files.
Gregory Pietsch. The only time include guards shouldn't really be used is if a header can be included multiple times in the same file and have different effects each time.
For example, I once designed a dynamic array header that I didn't put include guards in because it needed things defined to work and could be included multiple times in the same file for dynamic arrays of different types.
Still, it's the exception, not the rule. If an include file should not have include guards, the programmer should say so in the comments. I must have misunderstood something I read. Jordan Abel. I wouldn't call that a header - i'd maybe name it. Same with includable files that define data structures [unless it has a more specific suffix associated with it, such as. My hard disk thanks you You don't need the stuff in stdlib. I've never found a good answer to this one.
If you follow a policy of never allowing nested inclusions, then you have a list of dependencies at the top of each source file. You also have a list, in reverse order, of the files in call hierarchy. The problem is that quite often, in video games, you need a "world". Everything depends on the world, but it also contains everything.
Ian Collins. The waters are muddied further when the compiler supports some form of pre-compiled headers, where a global header that includes all headers can save a significant amount of compile time.
Malcolm wrote: That means that every file in the program includes "world. I don't understand what hierarchy has to do with it here. Include when you need a definition or declaration to be present - otherwise don't. You won't get any coding done if you fret about the hierarchy of header file includes.
Connect and share knowledge within a single location that is structured and easy to search. According to cppreference. First of all, I wonder why this is the case.
Isn't this in contradiction to the DRY principle? Does it matter at all? Assuming I wanted to minimize the functions and types I was importing I'd go with cstddef as it doesn't declare any functions and only declares 6 types.
The others focus on particular domains strings, time, IO that may not matter to you. In contrast, stddef. If you're planning to introduce a typedef in your code anyway i. Clarification: Try not to misinterpret 7. Extending the namespace std. All standard library header files have the same definition; it does not matter which one you include in your own code.
This file is included by every file you listed. The result of sizeof and sizeof Also note, that it is perfectly fine to make this typedef declaration in the global and in std namespace, as long as it matches all other typedef declarations of the same typedef-name a compiler error is issued on non-matching declarations. To sceptics saying that this constitutes an addition of a new type into namespace std , and such an act is explicitly prohibited by the standard, and this is UB and that is all there to it; I have to say that this attitude amounts to ignoring and denying deeper understanding of the underlying issues.
The standard bans adding new declarations and definitions into namespace std because by doing so the user may make a mess of the standard library and shoot his entire leg off. For the standard writers it was easier to let the user specialize a few specific things and ban doing anything else for good measure, rather than ban every single thing which the user should not do and risk missing something important and that leg.
They did it in the past when requiring that no standard container shall be instantiated with an incomplete type, while in fact some containers could well do see The Standard Librarian: Containers of Incomplete Types by Matthew H. Austern :. In the end, it all seemed too murky and too poorly understood; the standardization committee didn't think there was any choice except to say that STL containers aren't supposed to work with incomplete types.
For good measure, we applied that prohibition to the rest of the standard library too. In retrospect, now that the technology is better understood, that decision still seems basically right.
Yes, in some cases it's possible to implement some of the standard containers so that they can be instantiated with incomplete types — but it's also clear that in other cases it would be difficult or impossible.
It was mostly chance that the first test we tried, using std::vector , happened to be one of the easy cases. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Ask Question. Asked 5 years, 9 months ago. Active 9 months ago. Viewed 80k times. Open corresponding header files and find the definition. What was your point, exactly?
Show 10 more comments. Active Oldest Votes. Sean Sean SnakeDoc and as if by magic, another answer here has already observed exactly that happening, via an 'internal' header. SnakeDoc Yes, and that header is cstddef. SnakeDoc, who says they define their own? All the standard says is it will be defined after including those headers, it doesn't say they all have to redefine it.
Is the csttddef in the answer a typo?
0コメント