Including <exception> header C++ [duplicate] - c++

This question already has answers here:
Removing '#include <algorithm>' doesn't break the code
(7 answers)
Closed 6 years ago.
It seems when we include <iostream> header, <exception> and <stdexcept> headers are included automatically.
Question is why the reference sites like cppreference and cplusplus.com include <exception> while explaining exception handling?
Is it necessary to include <exception> or <stdexcept>?

You should always include what you use. The C++ standard doesn't state that any particular header has to include another, they are free to do so for convenience. But note that just because that happens to be the case for one compiler, it may not be the case on another (e.g. Visual Studio vs gcc)

You should always follow documentation. When documentation says that in order to use ceratain construct you need to include certain header, the header must be included. Otherwise, tomorrow iostream will stop including the header, and your program will fail to compile or worse - will behave unexpectedly.

Related

How does #include <bits/stdc++.h> work in C++? [duplicate]

This question already has answers here:
Why should I not #include <bits/stdc++.h>?
(9 answers)
Closed 4 years ago.
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?
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.
#include <bits/stdc++.h> is an implementation file for a precompiled header.
From a software engineering perspective, it is a good idea to minimize the include. If you use <bits/stdc++.h> it actually includes a lot of files, which your program may not need, thus increase both compile-time and program size unnecessarily. [edit: as pointed out by #Swordfish in the comments that the output program size remains unaffected. But still, it's good practice to include only the libraries you actually need, unless it's some competitive competition ]
But in contests, using this file is a good idea, when you want to reduce the time wasted in doing chores; especially when your rank is time-sensitive.
It works in most online judges, programming contest environments, including ACM-ICPC (Sub-Regionals, Regionals, and World Finals) and many online judges.
The disadvantages of it are that it:
increases the compilation time.
uses an internal non-standard header file of the GNU C++ library, and so will not compile in MSVC, XCode, and many other compilers
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.
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).

Using swap() without #including its library? [duplicate]

This question already has answers here:
C++ math functions can be used without including the directive "math.h" in VS 2013
(2 answers)
Closed 8 years ago.
I have a simple program in which I arrange the elements of an int array in ascending or descending order, and I use the swap() function to move the elements around. I compiled the program without any errors, and it ran like a charm. I only noticed afterwards that I had forgotten to #include the library that swap() is defined in (<algorithm>, or <utility> as of C++11) before I compiled.
Why did it still work? The top of my program looked like this:
#include <iostream>
#include <cstdlib>
using namespace std;
I tried taking out <iostream>, just to see what would happen, and it predictably put out a bunch of 'cout/cin/endl' was not declared in this scope errors, but I was surprised to see that it gave some 'swap' was not declared in this scope errors as well. Does that mean swap() is defined in <iostream>? I don't think it should be, should it?
Anyways, this is probably a big long question for a simple answer, but I'm pretty curious. I'm still learning C and C++, so I don't know a lot of things, and I couldn't find an answer to this particular mystery via the "Almighty" Google Machine, so here I am.
Thanks in advance!
Generally, do NOT rely on the header files that includes other header files.
Always include and only include the header files you need.
For example, if you want to use std::swap(), Google it and you'll see if requires <algorithm> in c++98 and <utility> in c++11, so you should include the file to make sure your code compiles.

What is the difference between including iostream and including iostream.h? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference between iostream and iostream.h
My professor said that the following:
#include <iostream.h>
is the same as:
#include <iostream>
using namespace std;
I'm a bit confused. What is the difference between iostream and iostream.h?
iostream.h is not part of the standard C++ library whereas iostream is. Names in iostream.h are not in the std namespace, whereas those in iostream are. By issuing the directive using namespace std after including iostream, all names defined there (and in any other standard library includes) are brought into the global namespace. That is usually not a good thing, but it does provide some level of equivalence between the standard and non- or pre-standard versions.
As far as claiming that they are "the same" as each other, this is unlikely. iostream adheres to the standard, and will have evolved w.r.t. iostream.h. This is particularly true if you consider the C++11 standard.
Some very old compilers have used iostream.h, but it's not part of the standard. Only the extensionless header files are. It won't even be recognized by modern standard-compliant compilers.

Can I rely on nested #includes? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C++ style question: what to #include?
When I #include a header file, and I also need other files that are already #included from the first one, should I rely on the first #include or should I #include all of them?
I know that it will work anyway, but I want to know what is the best practice.
If I don't rely, it means that I can have a list of few dozens of #includes in my file. Does it make sense?
Well, if someone else is maintaining the first header file, then no, you can't rely on it!
For exactly this reason, I prefer to explicitly include all dependencies (i.e. headers declaring symbols that are directly used) in source files. I doubt you'll find a One True Best Practice, though. There are pros and cons of each approach.
But once you choose an approach, please apply it consistently! There's nothing worse than a project with a mish-mash of different include styles.
That depends on the policy you design. I always follow the following one:
In headers, always include anything that is needed for that header to be compiled with a clean .c/.cpp file.
In implementation files, always include everything that is directly used.
You should include only the base header file ofcourse. but even if you happen to include your files, youe header files have inclusion gaurds which should prevnet from multiple inclusions.
When I #include a header file, and I
also need other files that are already
#included from the first one, should I rely on the first #include or should I
#include all of them?
In general no, because which header files a header file drags in, is in general an implementation detail.
However, it is in practice not possible to write code that will include all necessary headers for all platforms. Especially in C++ where standard library headers are free to drag in other headers from the standard library. For example, your code may compile because, unknown to you, <iostream> for your compiler drags in <string>.
So, a reasonable effort to include all relevant headers is reasonable, and as that implies, an unreasonable effort to do so, is unreasonable. :-)
Cheers & hth.,

C++ headers - Why would a program include both iostream and ostream? [duplicate]

This question already has answers here:
Why would I include iostream and ostream separately? [duplicate]
(2 answers)
Closed 6 years ago.
I saw some programs including <iostream> and <ostream> simultaneously. Why?
Thanks for your kind reply.
<iostream> is not the combination of <istream> and <ostream>. It merely defines std::cin, std::cout and related objects. To actually do anything useful with std::cout, you still need <ostream>. Now, by C++ rules it's possible that some implementations actually do include <ostream> in <iostream>, but you shouldn't rely on this.
Why? Probably because it originally included just ostream and someone figured out it had to use input streams as well. Then they just didn't bother to remove the ostream include.
Or maybe they actually needed the concrete cin/cout/cerr stream objects which are defined in iostream separately to the stuff in istream/ostream, and they didn't realise that iostream pulls in both istream and ostream before defining those objects.
Without asking the author, it's hard to say, but those are at least two viable possibilities.
Someone forgot to remove a header probably. You should always include only what you need in an implementation file but sometimes stuff gets left behind because people are lazy and/or don't know any better.
You should remove the one that is not needed.