3

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?

Community
  • 1
  • 1
Igor
  • 26,650
  • 27
  • 89
  • 114
  • When you say "header file", do you mean specifically standard headers (in which case this is a dupe) or any `#include` (in which case I'm not so sure, that other question concerns particular cases, not the general case)? – Steve Jessop Jun 22 '11 at 11:51
  • If non-dupe, then IMO it's a question of guarantees. a.h is either documented or otherwise guaranteed to include b.h (in which case no need to include it separately) or else it isn't (in which case you should include it separately, so that when a.h changes in future to not include b.h any more, your code doesn't break). – Steve Jessop Jun 22 '11 at 12:03

4 Answers4

5

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.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • The bad part is, that you sometimes miss a dependency of your file as you already have another include that already pulls in the required includes. – pmr Jun 22 '11 at 11:22
  • +1 to counter stupid anonymous downvoter. – Cheers and hth. - Alf Jun 22 '11 at 11:24
  • @pmr: Indeed. It's difficult to guarantee perfection with either approach, but IMHO it's something to aim for. I'm planning on investigating *include-what-you-use* (http://code.google.com/p/include-what-you-use/) to see if it can clean this sort of thing up. Haven't got round to it yet, though... – Oliver Charlesworth Jun 22 '11 at 11:26
  • 2
    Sometime back, I had raised a question in stackoverflow **to keep a compulsion to comment** for downvoting. Ppl mercilessly downvoted my question !! :| – iammilind Jun 22 '11 at 11:28
  • I don't see a need for compulsion to comment. I can upvote anonymously and rapidly, and nobody demands to know what I think is correct about the answer. It's nice to know what people think is wrong about your answer, but if they can't be bothered to tell you, and in particular in the case where somebody has already commented with the same criticism, they are already paying a rep point for the privilege of downvoting you. How many hoops and hurdles should someone have to negotiate in order to vote that you're wrong? Fix is to do what Oli did - ask for an explanation but otherwise suck it up! – Steve Jessop Jun 22 '11 at 12:08
  • @Steve: I think uncommented downvoting makes SO less serious. The very worst excesses can be caught by the mods (the personal vendetta downvoter). But the usual drive-by downvote slips trough. This downvote is almost never applied when there's already a number of upvotes. It doesn't just adjust the final sum. It is applied as the very first vote, causing the answer to appear at the bottom of the list, and causing others to not even read it, or to read it with a preconceived idea that it's Bad. So it's like sabotage. It's like those teenagers in "The Weatherman". It needs a fix. – Cheers and hth. - Alf Jun 22 '11 at 15:12
  • @Alf: I know that we can't see in retrospect what order the votes happened, but do you know any examples of correct answers which are downvoted, and incorrect answers that have "won" over them? On non-contentious issues, I mean, I'm not really interested whether or not the average "please may I use goto?" question suffers from people following the opinion of the first person to get a vote it ;-) My experience has been that if "the community" knows an answer is right, it'll get upvoted. If a correct answer remains on negative votes, it's because of some common misconception. – Steve Jessop Jun 22 '11 at 16:01
  • But I'm curious to see the kind of thing you mean, if you happen to remember particular instances of it. If not I'll keep an eye out for myself. – Steve Jessop Jun 22 '11 at 16:06
  • @Steve: I think the most infamous example was half a year ago (?) when a large number of impressionables over at Reddit (I think it was) decided to downvote a correct answer by one of the very high rep guys her -- I don't recall his name but I think he's mostly into MS technology. But that was so grotesque that he removed his answer or something. Mostly what happens is just a strong influence on normal voting, which is why I upvote to counter drive by downvotes AND ADD A COMMENT to that effect so that people may become more aware of the issue. – Cheers and hth. - Alf Jun 22 '11 at 16:19
2

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.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
1

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • **If** the headers that the base header includes, are part of the (documented or implicit) module interface. Otherwise you're in for a lot of extra work... :-( Sadly, with C++ lacking module support there's no practical way that I know of guaranteeing that one includes everything that's necessary for portability. – Cheers and hth. - Alf Jun 22 '11 at 11:23
1

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.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331