C++ why include related header first?
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
Related to this question: C/C++ include file order/best practices
The best answer recommends to include the related header first.
Same for Google and Mozilla style guides.
However, in both cases, I couldn't find a good reason why you would do this.
Google and Mozilla coding rules look best to me, because they enforce you to include the most "standard" headers first.
This way, I think you are less likely to mess up the included files (for examples by undefining some macros used in the other headers etc.) so that looks like the best way to go for me.
But following that rationale, why would you include the related header first? Since any syntax error in it might mess all the following headers?
I would think including the related header last would be best instead.
c++ header-files
add a comment |Â
up vote
6
down vote
favorite
Related to this question: C/C++ include file order/best practices
The best answer recommends to include the related header first.
Same for Google and Mozilla style guides.
However, in both cases, I couldn't find a good reason why you would do this.
Google and Mozilla coding rules look best to me, because they enforce you to include the most "standard" headers first.
This way, I think you are less likely to mess up the included files (for examples by undefining some macros used in the other headers etc.) so that looks like the best way to go for me.
But following that rationale, why would you include the related header first? Since any syntax error in it might mess all the following headers?
I would think including the related header last would be best instead.
c++ header-files
2
I fail to see why it's downvoted. The OP presents research, and asks about something they did not understand from conducting it. There is a well defined answer as well. All in all, it fits the criteria of a decent StackOverflow question.
– StoryTeller
50 mins ago
1
@StoryTeller didnt downvote, though the google guidelines give more rationale than what the question suggests: "With the preferred ordering, if dir2/foo2.h omits any necessary includes, the build of dir/foo.cc or dir/foo_test.cc will break. Thus, this rule ensures that build breaks show up first for the people working on these files, not for innocent people in other packages." this is more or less the same what the answers are repeating here
– user463035818
45 mins ago
@user463035818 - Updated my answer accordingly. Google prohibits forward declarations IIRC, so that's a bit of rationale they won't post about.
– StoryTeller
41 mins ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
Related to this question: C/C++ include file order/best practices
The best answer recommends to include the related header first.
Same for Google and Mozilla style guides.
However, in both cases, I couldn't find a good reason why you would do this.
Google and Mozilla coding rules look best to me, because they enforce you to include the most "standard" headers first.
This way, I think you are less likely to mess up the included files (for examples by undefining some macros used in the other headers etc.) so that looks like the best way to go for me.
But following that rationale, why would you include the related header first? Since any syntax error in it might mess all the following headers?
I would think including the related header last would be best instead.
c++ header-files
Related to this question: C/C++ include file order/best practices
The best answer recommends to include the related header first.
Same for Google and Mozilla style guides.
However, in both cases, I couldn't find a good reason why you would do this.
Google and Mozilla coding rules look best to me, because they enforce you to include the most "standard" headers first.
This way, I think you are less likely to mess up the included files (for examples by undefining some macros used in the other headers etc.) so that looks like the best way to go for me.
But following that rationale, why would you include the related header first? Since any syntax error in it might mess all the following headers?
I would think including the related header last would be best instead.
c++ header-files
c++ header-files
edited 10 mins ago
StoryTeller
84.5k12169236
84.5k12169236
asked 58 mins ago
Arnaud
1205
1205
2
I fail to see why it's downvoted. The OP presents research, and asks about something they did not understand from conducting it. There is a well defined answer as well. All in all, it fits the criteria of a decent StackOverflow question.
– StoryTeller
50 mins ago
1
@StoryTeller didnt downvote, though the google guidelines give more rationale than what the question suggests: "With the preferred ordering, if dir2/foo2.h omits any necessary includes, the build of dir/foo.cc or dir/foo_test.cc will break. Thus, this rule ensures that build breaks show up first for the people working on these files, not for innocent people in other packages." this is more or less the same what the answers are repeating here
– user463035818
45 mins ago
@user463035818 - Updated my answer accordingly. Google prohibits forward declarations IIRC, so that's a bit of rationale they won't post about.
– StoryTeller
41 mins ago
add a comment |Â
2
I fail to see why it's downvoted. The OP presents research, and asks about something they did not understand from conducting it. There is a well defined answer as well. All in all, it fits the criteria of a decent StackOverflow question.
– StoryTeller
50 mins ago
1
@StoryTeller didnt downvote, though the google guidelines give more rationale than what the question suggests: "With the preferred ordering, if dir2/foo2.h omits any necessary includes, the build of dir/foo.cc or dir/foo_test.cc will break. Thus, this rule ensures that build breaks show up first for the people working on these files, not for innocent people in other packages." this is more or less the same what the answers are repeating here
– user463035818
45 mins ago
@user463035818 - Updated my answer accordingly. Google prohibits forward declarations IIRC, so that's a bit of rationale they won't post about.
– StoryTeller
41 mins ago
2
2
I fail to see why it's downvoted. The OP presents research, and asks about something they did not understand from conducting it. There is a well defined answer as well. All in all, it fits the criteria of a decent StackOverflow question.
– StoryTeller
50 mins ago
I fail to see why it's downvoted. The OP presents research, and asks about something they did not understand from conducting it. There is a well defined answer as well. All in all, it fits the criteria of a decent StackOverflow question.
– StoryTeller
50 mins ago
1
1
@StoryTeller didnt downvote, though the google guidelines give more rationale than what the question suggests: "With the preferred ordering, if dir2/foo2.h omits any necessary includes, the build of dir/foo.cc or dir/foo_test.cc will break. Thus, this rule ensures that build breaks show up first for the people working on these files, not for innocent people in other packages." this is more or less the same what the answers are repeating here
– user463035818
45 mins ago
@StoryTeller didnt downvote, though the google guidelines give more rationale than what the question suggests: "With the preferred ordering, if dir2/foo2.h omits any necessary includes, the build of dir/foo.cc or dir/foo_test.cc will break. Thus, this rule ensures that build breaks show up first for the people working on these files, not for innocent people in other packages." this is more or less the same what the answers are repeating here
– user463035818
45 mins ago
@user463035818 - Updated my answer accordingly. Google prohibits forward declarations IIRC, so that's a bit of rationale they won't post about.
– StoryTeller
41 mins ago
@user463035818 - Updated my answer accordingly. Google prohibits forward declarations IIRC, so that's a bit of rationale they won't post about.
– StoryTeller
41 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
9
down vote
accepted
It's to make sure your clients don't hate you when they include your library header.
If the header is brittle and subject to break on wrong inclusion order, it may appear to work in your environment when it isn't first - since you include the headers you need - but fail to compile for client code. Because it may not at all be obvious what other headers need to be pulled in for it to work.
Including first the header which corresponds to the current implementation file goes toward checking that the header is self-contained. Self-containment goes beyond just including the necessary headers. It also entails adding the required forward declarations for types you use in your API. Naturally your header will compile if you include the header for the type before it, but you may not wish to pull it in since you only depend on the type name in your API.
Some style guides prohibit forward declarations, so those may not be part of the rationale they pose.
add a comment |Â
up vote
3
down vote
Including the header related to the cpp file first ensures that the header is self contained and doesn't require other includes to compile
2
Why the downvote? May be a bit short, but is correct.
– Rene
53 mins ago
1
"ensures that the header is self contained" - ensuring this is not possible in general. Situation when header file may be not self contained but works on current platform because on that platform required header gets included indirectly is quite typical and yet difficult to detect until you actually get an error. Including project headers prior to library headers may indeed help to detect whether related header is not self contained, but may not ensure it.
– VTT
41 mins ago
1
@VTT - Ensure? No. Provide some checks which are better than none? Yes.
– StoryTeller
40 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
It's to make sure your clients don't hate you when they include your library header.
If the header is brittle and subject to break on wrong inclusion order, it may appear to work in your environment when it isn't first - since you include the headers you need - but fail to compile for client code. Because it may not at all be obvious what other headers need to be pulled in for it to work.
Including first the header which corresponds to the current implementation file goes toward checking that the header is self-contained. Self-containment goes beyond just including the necessary headers. It also entails adding the required forward declarations for types you use in your API. Naturally your header will compile if you include the header for the type before it, but you may not wish to pull it in since you only depend on the type name in your API.
Some style guides prohibit forward declarations, so those may not be part of the rationale they pose.
add a comment |Â
up vote
9
down vote
accepted
It's to make sure your clients don't hate you when they include your library header.
If the header is brittle and subject to break on wrong inclusion order, it may appear to work in your environment when it isn't first - since you include the headers you need - but fail to compile for client code. Because it may not at all be obvious what other headers need to be pulled in for it to work.
Including first the header which corresponds to the current implementation file goes toward checking that the header is self-contained. Self-containment goes beyond just including the necessary headers. It also entails adding the required forward declarations for types you use in your API. Naturally your header will compile if you include the header for the type before it, but you may not wish to pull it in since you only depend on the type name in your API.
Some style guides prohibit forward declarations, so those may not be part of the rationale they pose.
add a comment |Â
up vote
9
down vote
accepted
up vote
9
down vote
accepted
It's to make sure your clients don't hate you when they include your library header.
If the header is brittle and subject to break on wrong inclusion order, it may appear to work in your environment when it isn't first - since you include the headers you need - but fail to compile for client code. Because it may not at all be obvious what other headers need to be pulled in for it to work.
Including first the header which corresponds to the current implementation file goes toward checking that the header is self-contained. Self-containment goes beyond just including the necessary headers. It also entails adding the required forward declarations for types you use in your API. Naturally your header will compile if you include the header for the type before it, but you may not wish to pull it in since you only depend on the type name in your API.
Some style guides prohibit forward declarations, so those may not be part of the rationale they pose.
It's to make sure your clients don't hate you when they include your library header.
If the header is brittle and subject to break on wrong inclusion order, it may appear to work in your environment when it isn't first - since you include the headers you need - but fail to compile for client code. Because it may not at all be obvious what other headers need to be pulled in for it to work.
Including first the header which corresponds to the current implementation file goes toward checking that the header is self-contained. Self-containment goes beyond just including the necessary headers. It also entails adding the required forward declarations for types you use in your API. Naturally your header will compile if you include the header for the type before it, but you may not wish to pull it in since you only depend on the type name in your API.
Some style guides prohibit forward declarations, so those may not be part of the rationale they pose.
edited 42 mins ago
answered 55 mins ago
StoryTeller
84.5k12169236
84.5k12169236
add a comment |Â
add a comment |Â
up vote
3
down vote
Including the header related to the cpp file first ensures that the header is self contained and doesn't require other includes to compile
2
Why the downvote? May be a bit short, but is correct.
– Rene
53 mins ago
1
"ensures that the header is self contained" - ensuring this is not possible in general. Situation when header file may be not self contained but works on current platform because on that platform required header gets included indirectly is quite typical and yet difficult to detect until you actually get an error. Including project headers prior to library headers may indeed help to detect whether related header is not self contained, but may not ensure it.
– VTT
41 mins ago
1
@VTT - Ensure? No. Provide some checks which are better than none? Yes.
– StoryTeller
40 mins ago
add a comment |Â
up vote
3
down vote
Including the header related to the cpp file first ensures that the header is self contained and doesn't require other includes to compile
2
Why the downvote? May be a bit short, but is correct.
– Rene
53 mins ago
1
"ensures that the header is self contained" - ensuring this is not possible in general. Situation when header file may be not self contained but works on current platform because on that platform required header gets included indirectly is quite typical and yet difficult to detect until you actually get an error. Including project headers prior to library headers may indeed help to detect whether related header is not self contained, but may not ensure it.
– VTT
41 mins ago
1
@VTT - Ensure? No. Provide some checks which are better than none? Yes.
– StoryTeller
40 mins ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Including the header related to the cpp file first ensures that the header is self contained and doesn't require other includes to compile
Including the header related to the cpp file first ensures that the header is self contained and doesn't require other includes to compile
answered 55 mins ago
Alan Birtles
5,964531
5,964531
2
Why the downvote? May be a bit short, but is correct.
– Rene
53 mins ago
1
"ensures that the header is self contained" - ensuring this is not possible in general. Situation when header file may be not self contained but works on current platform because on that platform required header gets included indirectly is quite typical and yet difficult to detect until you actually get an error. Including project headers prior to library headers may indeed help to detect whether related header is not self contained, but may not ensure it.
– VTT
41 mins ago
1
@VTT - Ensure? No. Provide some checks which are better than none? Yes.
– StoryTeller
40 mins ago
add a comment |Â
2
Why the downvote? May be a bit short, but is correct.
– Rene
53 mins ago
1
"ensures that the header is self contained" - ensuring this is not possible in general. Situation when header file may be not self contained but works on current platform because on that platform required header gets included indirectly is quite typical and yet difficult to detect until you actually get an error. Including project headers prior to library headers may indeed help to detect whether related header is not self contained, but may not ensure it.
– VTT
41 mins ago
1
@VTT - Ensure? No. Provide some checks which are better than none? Yes.
– StoryTeller
40 mins ago
2
2
Why the downvote? May be a bit short, but is correct.
– Rene
53 mins ago
Why the downvote? May be a bit short, but is correct.
– Rene
53 mins ago
1
1
"ensures that the header is self contained" - ensuring this is not possible in general. Situation when header file may be not self contained but works on current platform because on that platform required header gets included indirectly is quite typical and yet difficult to detect until you actually get an error. Including project headers prior to library headers may indeed help to detect whether related header is not self contained, but may not ensure it.
– VTT
41 mins ago
"ensures that the header is self contained" - ensuring this is not possible in general. Situation when header file may be not self contained but works on current platform because on that platform required header gets included indirectly is quite typical and yet difficult to detect until you actually get an error. Including project headers prior to library headers may indeed help to detect whether related header is not self contained, but may not ensure it.
– VTT
41 mins ago
1
1
@VTT - Ensure? No. Provide some checks which are better than none? Yes.
– StoryTeller
40 mins ago
@VTT - Ensure? No. Provide some checks which are better than none? Yes.
– StoryTeller
40 mins ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52716773%2fc-why-include-related-header-first%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
I fail to see why it's downvoted. The OP presents research, and asks about something they did not understand from conducting it. There is a well defined answer as well. All in all, it fits the criteria of a decent StackOverflow question.
– StoryTeller
50 mins ago
1
@StoryTeller didnt downvote, though the google guidelines give more rationale than what the question suggests: "With the preferred ordering, if dir2/foo2.h omits any necessary includes, the build of dir/foo.cc or dir/foo_test.cc will break. Thus, this rule ensures that build breaks show up first for the people working on these files, not for innocent people in other packages." this is more or less the same what the answers are repeating here
– user463035818
45 mins ago
@user463035818 - Updated my answer accordingly. Google prohibits forward declarations IIRC, so that's a bit of rationale they won't post about.
– StoryTeller
41 mins ago