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.
Related
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.
This question already has answers here:
When can I use a forward declaration?
(13 answers)
Closed 7 years ago.
So i've just read about forward declarations at various sources (e.g googles c++ style guide) and i am quite confused when i should and when i shouldn't use forward declarations.
One one hand if I always forward declare classes i will never have troubles with circular dependencies and faster compile times.
But on the other hand i will have to use almost exclusively work with pointers to objects which seems quite unsafe considering memory leaks and also unnecessary complicated to work with.
So should i use forward declarations whenever I possibly can or only when its needed to avoid stuff like circular dependencies?
And another question regarding forward declarations. If im working with extern libraries such as GLM (which is a math library) and i need it in many different classes is there a way to forward declare those aswell / does it even make sense to do so?
Examples of what i have to include (GLM):
#include <glm.hpp>
#include <gtc/matrix_transform.hpp>
#include <gtc/type_ptr.hpp>
And for example i will use it like this:
std::vector <glm::vec3> vertices;
Generally forward declarations end up being necessary only if there is some sort of circular dependency, and generally should be avoided.
So the version that says 'only for circular dependencies' is more on the right track.
"Pointers to objects" is not really relevant to the question, as in modern C++ normally raw pointers should be avoided most of the time anyway, using one of the now standard smart pointers instead, or better yet, references. Depending on what you are trying to do, keeping class members out of the class headers using Pimpl or fast pimpl idioms might be a good practice as well.
To last part of your question, what you probably want to do is to include the dependency external library headers that you commonly use in a single header file of your own, and then just include this file in your code modules where required. This can potentially give an advantage in compilation times too, if you have set up to use precompiled headers.
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.
If I have multiple files that #include each other, and all #include <iostream>, is this considered bad, and if so, how would I avoid it?
No, there is nothing wrong with it. Every file that needs to directly use functionality from <iostream>, should include it directly. Header guards will take care of multiple inclusions.
There is a potential problem when you have circular dependencies. For example, see this question: Resolve header include circular dependencies
However, since <iostream> is unlikely to be including or depending on any of your files, circular dependencies are not a problem in this case.
The first question is whether you really need to include iostream. In most cases headers don't really need iostream, but the lesser ostream (no need for cin, cout... just the type std::ostream& for operator<<). Even there, the correct header would be iosfwd which contains just forward declarations of those elements.
That is, of course, unless you need the full declarations for the types or the real iostreams... then just include them.
No, this is not a problem. I have never heard at least of any.
Pre-processor should do the required work and I think it's also good style to have every class/sourcefile needing <iostream> to include it.
Therefor everyone knows that this file uses functionality provided by iostream.
By the way: using namespace std; should in any case be avoided to ensure everyone sees the corresponding used namespace.
There's no problem is doing this, there is include guard, which ensures just one time inclusion of the standard header file
Not an issue as long as you make sure you don't have too many headers stacked in each other. Too many and certain OS can't handle it, namely older ones. But unless you got an ancient computer with very old software, it should be perfectly fine! Good Luck to you!
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.,