{"id":522,"date":"2010-08-17T23:06:22","date_gmt":"2010-08-17T23:06:22","guid":{"rendered":"http:\/\/kera.name\/articles\/?p=522"},"modified":"2011-06-22T09:27:22","modified_gmt":"2011-06-22T09:27:22","slug":"tomalaks-tuesday-tip-6-an-inclusion-pitfall","status":"publish","type":"post","link":"https:\/\/kera.name\/articles\/2010\/08\/tomalaks-tuesday-tip-6-an-inclusion-pitfall\/","title":{"rendered":"Tomalak&#039;s Tuesday Tip #6: An Inclusion Pitfall"},"content":{"rendered":"<p>One of the evilnesses of the C++ Standard Library is that implementations &mdash; be they GCC&#039;s libstdc++ or the Dimkumware implementation used by MSVC &mdash; are free to have their headers include other standard headers as much as they like. This can lead to confusion. For example, the oft-given:<\/p>\n<p><textarea name=\"code\" class=\"cpp:nocontrols:nogutter\" cols=\"60\" rows=\"10\">#include <iostream><\/p>\n<p>int main() {\nstd::cout << \"Hello world\\n\";\n}<\/textarea>\n<\/p>\n<p>This is technically incorrect!<\/p>\n<p>Although the most popular standard library implementations currently <i>just so happen<\/i> to have an <code>iostream<\/code> implementation that itself includes <code>ostream<\/code> (for any internal reason), you should include the header <code>ostream<\/code> yourself to gain access to <code>ostream<\/code>&#039;s <code>operator&lt;&lt;<\/code> that you use above. If one day your toolchain&#039;s code gets restructured and iostream no longer includes <code>ostream<\/code>, your build will break because you&#039;ve included the wrong header.<\/p>\n<p>Unlikey though it is to be a problem in this specific case, it&#039;s important in general to include precisely the right header for the job according to the documentation, and not one that just seems to work for you at any given time. Even if you don&#039;t care about immediate portability or proper correctness, then at least think about future maintainability.<\/p>\n<p>The corrected snippet reads:<\/p>\n<p><textarea name=\"code\" class=\"cpp:nocontrols:nogutter\" cols=\"60\" rows=\"10\">#include <iostream> \/\/ for std::cout<\/p>\n<h1>include <ostream>  \/\/ for std::ostream::operator<<<\/h1>\n<p>int main() {\nstd::cout << \"Hello world\\n\";\n}<\/textarea><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the evilnesses of the C++ Standard Library is that implementations &mdash; be they GCC&#039;s libstdc++ or the Dimkumware implementation used by MSVC &mdash; are free to have their headers include other standard headers as much as they like. This can lead to confusion.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[21,20,12,33],"_links":{"self":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/522"}],"collection":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/comments?post=522"}],"version-history":[{"count":10,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/522\/revisions"}],"predecessor-version":[{"id":719,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/522\/revisions\/719"}],"wp:attachment":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/media?parent=522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/categories?post=522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/tags?post=522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}