Why include related header first?
Clash Royale CLAN TAG#URR8PPP
up vote
11
down vote
favorite
On the 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
 |Â
show 2 more comments
up vote
11
down vote
favorite
On the 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
4
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
2 hours ago
3
@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
2 hours ago
@user463035818 - Updated my answer accordingly. Google prohibits forward declarations IIRC, so that's a bit of rationale they won't post about.
– StoryTeller
2 hours ago
The use of the term "related" is quite vague. All includes should be "related" otherwise why have them? Please correct this, possibly using the original term from the question you cited, "corresponding".
– Lightness Races in Orbit
1 hour ago
@StoryTeller They don’t forbid forward declarations, and this would severely limit the set of correct, useful code they could write (any kind of circularity requires forward declarations unless you erase all types). They limit the use to those cases where it’s necessary.
– Konrad Rudolph
13 mins ago
 |Â
show 2 more comments
up vote
11
down vote
favorite
up vote
11
down vote
favorite
On the 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
On the 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 15 mins ago
Konrad Rudolph
387k1007611012
387k1007611012
asked 2 hours ago
Arnaud
1456
1456
4
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
2 hours ago
3
@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
2 hours ago
@user463035818 - Updated my answer accordingly. Google prohibits forward declarations IIRC, so that's a bit of rationale they won't post about.
– StoryTeller
2 hours ago
The use of the term "related" is quite vague. All includes should be "related" otherwise why have them? Please correct this, possibly using the original term from the question you cited, "corresponding".
– Lightness Races in Orbit
1 hour ago
@StoryTeller They don’t forbid forward declarations, and this would severely limit the set of correct, useful code they could write (any kind of circularity requires forward declarations unless you erase all types). They limit the use to those cases where it’s necessary.
– Konrad Rudolph
13 mins ago
 |Â
show 2 more comments
4
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
2 hours ago
3
@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
2 hours ago
@user463035818 - Updated my answer accordingly. Google prohibits forward declarations IIRC, so that's a bit of rationale they won't post about.
– StoryTeller
2 hours ago
The use of the term "related" is quite vague. All includes should be "related" otherwise why have them? Please correct this, possibly using the original term from the question you cited, "corresponding".
– Lightness Races in Orbit
1 hour ago
@StoryTeller They don’t forbid forward declarations, and this would severely limit the set of correct, useful code they could write (any kind of circularity requires forward declarations unless you erase all types). They limit the use to those cases where it’s necessary.
– Konrad Rudolph
13 mins ago
4
4
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
2 hours 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
2 hours ago
3
3
@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
2 hours 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
2 hours ago
@user463035818 - Updated my answer accordingly. Google prohibits forward declarations IIRC, so that's a bit of rationale they won't post about.
– StoryTeller
2 hours ago
@user463035818 - Updated my answer accordingly. Google prohibits forward declarations IIRC, so that's a bit of rationale they won't post about.
– StoryTeller
2 hours ago
The use of the term "related" is quite vague. All includes should be "related" otherwise why have them? Please correct this, possibly using the original term from the question you cited, "corresponding".
– Lightness Races in Orbit
1 hour ago
The use of the term "related" is quite vague. All includes should be "related" otherwise why have them? Please correct this, possibly using the original term from the question you cited, "corresponding".
– Lightness Races in Orbit
1 hour ago
@StoryTeller They don’t forbid forward declarations, and this would severely limit the set of correct, useful code they could write (any kind of circularity requires forward declarations unless you erase all types). They limit the use to those cases where it’s necessary.
– Konrad Rudolph
13 mins ago
@StoryTeller They don’t forbid forward declarations, and this would severely limit the set of correct, useful code they could write (any kind of circularity requires forward declarations unless you erase all types). They limit the use to those cases where it’s necessary.
– Konrad Rudolph
13 mins ago
 |Â
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
18
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
6
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
3
Why the downvote? May be a bit short, but is correct.
– Rene
2 hours 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
2 hours ago
1
@VTT - Ensure? No. Provide some checks which are better than none? Yes.
– StoryTeller
2 hours ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
18
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
18
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
18
down vote
accepted
up vote
18
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 2 hours ago
answered 2 hours ago
StoryTeller
84.6k12169237
84.6k12169237
add a comment |Â
add a comment |Â
up vote
6
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
3
Why the downvote? May be a bit short, but is correct.
– Rene
2 hours 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
2 hours ago
1
@VTT - Ensure? No. Provide some checks which are better than none? Yes.
– StoryTeller
2 hours ago
add a comment |Â
up vote
6
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
3
Why the downvote? May be a bit short, but is correct.
– Rene
2 hours 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
2 hours ago
1
@VTT - Ensure? No. Provide some checks which are better than none? Yes.
– StoryTeller
2 hours ago
add a comment |Â
up vote
6
down vote
up vote
6
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 2 hours ago
Alan Birtles
5,994531
5,994531
3
Why the downvote? May be a bit short, but is correct.
– Rene
2 hours 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
2 hours ago
1
@VTT - Ensure? No. Provide some checks which are better than none? Yes.
– StoryTeller
2 hours ago
add a comment |Â
3
Why the downvote? May be a bit short, but is correct.
– Rene
2 hours 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
2 hours ago
1
@VTT - Ensure? No. Provide some checks which are better than none? Yes.
– StoryTeller
2 hours ago
3
3
Why the downvote? May be a bit short, but is correct.
– Rene
2 hours ago
Why the downvote? May be a bit short, but is correct.
– Rene
2 hours 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
2 hours 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
2 hours ago
1
1
@VTT - Ensure? No. Provide some checks which are better than none? Yes.
– StoryTeller
2 hours ago
@VTT - Ensure? No. Provide some checks which are better than none? Yes.
– StoryTeller
2 hours 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%2fwhy-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
4
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
2 hours ago
3
@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
2 hours ago
@user463035818 - Updated my answer accordingly. Google prohibits forward declarations IIRC, so that's a bit of rationale they won't post about.
– StoryTeller
2 hours ago
The use of the term "related" is quite vague. All includes should be "related" otherwise why have them? Please correct this, possibly using the original term from the question you cited, "corresponding".
– Lightness Races in Orbit
1 hour ago
@StoryTeller They don’t forbid forward declarations, and this would severely limit the set of correct, useful code they could write (any kind of circularity requires forward declarations unless you erase all types). They limit the use to those cases where it’s necessary.
– Konrad Rudolph
13 mins ago