Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid re-including <iostream> in multiple files?

Tags:

c++

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?

like image 362
iobender Avatar asked Jan 07 '14 19:01

iobender


People also ask

Do I need to include iostream in every file?

No, there is nothing wrong with it. Every file that needs to directly use functionality from <iostream> , should include it directly.

How do you prevent the same header file being included multiple times in C++?

To avoid multiple inclusions of the same header file we use the #ifndef, #define and #endif preprocessor directives. Just write the entire program in only file and include headers once only.

What happens if we include a header file twice C++?

If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time. This construct is commonly known as a wrapper #ifndef.

Do I need to include iostream in header file?

OTOH, if the user of the iostream is strictly hidden so the client code doesn't need to include it at all, then it should only be included in the implementation file. In many cases (especially where the header is open to frequent change), you'd prefer to avoid including it in the header.

Is it safe to include iostream multiple times in a header?

Yes, safely. All standard headers have include guards so to allow this usage safely. Will using include guards yourself in your code prevent iostream from be included multiple times?

Can you include iostream multiple times in the same compilation unit?

Can you include iostream multiple times in the same compilation unit? Yes, safely. All standard headers have include guards so to allow this usage safely. Will using include guards yourself in your code prevent iostream from be included multiple times?

Is there an include guard for iostream headers?

#ifndef __IOSTREAM_H #include <iostream> #endif ... Seems that most standard headers have a sort of include guard as above. Alternately you could make your own include guard by defining the required macro.

Will ifndef and define prevent multiple inclusions in iostream?

But that is not really needed for #include <iostream>, because it is already guarded from multiple inclusions. Show activity on this post. Like chris and Haroogan said, yes they will prevent that. What the #ifndef and #define are are pre-processor instructions, and are translated in english to


1 Answers

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.

like image 101
Benjamin Lindley Avatar answered Sep 18 '22 22:09

Benjamin Lindley