Why is C++ so dumb?

At this point in C++'s lifetime, why are include guards not automatic behavior? Since we have to put them on every single file anyways, why is this not just something that the compiler takes care of for us? On any non-trivial application, it's a source of annoying to find bugs.

Is this just technically infeasible?
Although many include files are intended to only be included once per source file, that is not always the case. Include files may include all sorts of things beyond macros, externs, class definitions, and templates. I have seen many include files used for static initialization (when the initialization list is too large for a macro or when it contains conditional information). For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
uint32_t ImageRed[] =
{
    IMAGE_HEADER,
    #define PIXEL  0x00FF0000
    #include "image_bitmap"
    #undef PIXEL 
};

uint32_t ImageBlue[] =
{
   IMAGE_HEADER,
    #define PIXEL 0x000000FF
    #include "image_bitmap"
    #undef PIXEL
};

Automatically ignoring the second image_bitmap inclusion would silently introduce a bug where ImageBlue[] now has no image data.

I am not saying that having automatic include guards is a bad idea, but there are compatibility issues which need to be considered. A more likely option is to extend the language ... for example:
#include once "header.hpp"
On any non-trivial application, it's a source of annoying to find bugs.
I don't think I've ever encountered a bug that was in any way related to a header being included twice.
I don't see why we still need header files at all. C# and Java don't have them, or even anything analogous to them (import and using are more analogous to C++'s using directive).
There is work being done on modules instead of copy-paste headers.

D actually uses modules. You can look at how it does it to see what C++ might do in the future. Although it is kinda weird whenever you deal with templates and similar.
Speaking of which, I have had bugs with doubly included headers. But they're generally very trivial to handle and understand. I'm often amazed as to what people will do before they look for a header without a guard though...

On a second note, I use #pragma once given that it's implemented on just about every single compiler that I know of.
I remember having an issue of a doubly included header which had a guard, but there was a small typo. I didn't see it for awhile.

AFAIK, #pragma once is not standard though, right?

I like the #include once "header" idea. Or to just get rid of headers entirely.
#pragma once isn't standard but when it's support by literally every compiler you will ever see, it's practically as reliable as the standard. Hell, even the standard isn't that reliable in certain cases. It's not like every C++ compiler conforms to C++11 yet. Maybe in another 3 or so years, and then we'll be going through the same process with C++14.
Sorry for replying to an old thread, but had made a note to comment on the include header guard and then forgot about it. If you don't want to be bothered by typing in three lines for your headers just use your editor. Most editors allow plugins written or templates made. I use Code::Blocks for my projects and it has a option to do the header guards for you : http://youtu.be/Pjff3fKbyV8

Though, don't get me wrong, I have no trouble just typing up 3 lines of code, but I know some don't care for doing so or forget to so routinely it becomes an annoyance.

[EDIT] Typos
Last edited on
It's not that it's this huge inconvenience to type it, just that with 95% of the time you HAVE to write it, it seems logical that this behavior would just be the default behavior of the language.
Last edited on
Topic archived. No new replies allowed.