177

I have read from a codeforces blog that if we add #include <bits/stdc++.h> in a C++ program then there is no need to include any other header files. How does #include <bits/stdc++.h> work and is it ok to use it instead of including individual header files?

6
  • 5
    Most likely this is for educational purposes. I assume that bits/stdc++.h includes all C++ headers. Commented Aug 14, 2014 at 14:48
  • 5
    From the header source itself: This is an implementation file for a precompiled header. Commented Aug 14, 2014 at 14:57
  • 3
    @MarcoA. Because computers in 1980 had less memory. Commented Aug 14, 2014 at 14:57
  • 71
    @NeilKirk: I used that tool you mentioned, followed the first link, arrived to this page, then saw your comment, and got stuck in a loop. Commented Oct 28, 2014 at 22:29
  • 3
    There was an effort way back by none other than BS to essentially standardize a kitchen sink header for new users and for quick toy builds. It died somehow (bad practice for large real projects? I also remember they wanted release and debug to have the same ABI which was unworkable). I can't find the reference paper though. I think there is a legitimate niche for such a header though. Although this should all magically go away when we get modules. Commented Aug 4, 2015 at 19:35

3 Answers 3

176

It is basically a header file that also includes every standard library and STL include file. The only purpose I can see for it would be for testing and education.

Se e.g. GCC 4.8.0 /bits/stdc++.h source.

Using it would include a lot of unnecessary stuff and increases compilation time.

Edit: As Neil says, it's an implementation for precompiled headers. If you set it up for precompilation correctly it could, in fact, speed up compilation time depending on your project. (https://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html)

I would, however, suggest that you take time to learn about each of the sl/stl headers and include them separately instead, and not use "super headers" except for precompilation purposes.

Sign up to request clarification or add additional context in comments.

8 Comments

It's for precompiled headers, which decrease compile time! (When set up right)
I don't think it's very good to learn C++ totally unaware of where things you're using are defined.. at what point do you stop and figure it out?
@OllieFord Never, ideally. This is 21st century. Compilers should figure out what parts of the standard library I need, to save me time to code important stuff. But of course people should learn about the headers, as it is part of the language.
No, but depending on student age and curriculum and so forth ...
A slight disadvantage of including it is that it results in having many names in the namespace. On rare occasions this could lead to some hard-to-understand bugs (for a few seconds). For example if one of your variables is named count. But I can't think of a specific confusing error example at the moment...
|
41

That header file is not part of the C++ standard, is therefore non-portable, and should be avoided.

Moreover, even if there were some catch-all header in the standard, you would want to avoid it in lieu of specific headers, since the compiler has to actually read in and parse every included header (including recursively included headers) every single time that translation unit is compiled.

5 Comments

That's what precompiled headers are for. I have one with pretty much the whole standard library in it. That's just how roll.
@NeilKirk: Sounds non-standard to me.
No it's not. It's an option on most compiler to speed it up.
By definition "most compilers" is non standard - if it were standard then it would be "all compilers" I don't see anywhere in the standard where it talks about how headers are precompiled...
If we avoided everything that wasn't part of the standard, then we would rewrite everything for every project. At some point using other people's libraries is a good idea even if they aren't part of the standard.
7

Unfortunately that approach is not portable C++ (so far).

All standard names are in namespace std and moreover you cannot know which names are NOT defined by including and header (in other words it's perfectly legal for an implementation to declare the name std::string directly or indirectly when using #include <vector>).

Despite this however you are required by the language to know and tell the compiler which standard header includes which part of the standard library. This is a source of portability bugs because if you forget for example #include <map> but use std::map it's possible that the program compiles anyway silently and without warnings on a specific version of a specific compiler, and you may get errors only later when porting to another compiler or version.

In my opinion there are no valid technical excuses that explain why this is necessary for the general user: the compiler binary could have all standard namespace built in and this could actually increase the performance even more than precompiled headers (e.g. using perfect hashing for lookups, removing standard headers parsing or loading/demarshalling and so on).

The use of standard headers simplifies the life of who builds compilers or standard libraries and that's all. It's not something to help users.

However this is the way the language is defined and you need to know which header defines which names so plan for some extra neurons to be burnt in pointless configurations to remember that (or try to find and IDE that automatically adds the standard headers you use and removes the ones you don't... a reasonable alternative).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.