-
Why doesn't the compiler do that for every header file? . . . .
It could use the timestamp to determine if the header file has been modified since it was last opened. Though only rather complex build processes would modify header files in this way, I should imagine.
-
I don't know. Seems kind of silly.
But Microsoft aren't known for making sense.
-
Timestamps? Huh? Do you expect headers to change during a compilation?
Nah, the "open only once" doesn't carry across compiles. It's more about this situation:
Code:
// a.h
// stuff
// b.h
#include "a.h"
// c.h
#include "a.h"
// main.cpp
#include "b.h"
#include "c.h"
If the headers are protected by the typical guards, then the MS compiler will open a.h twice, only to discover the second time that there's nothing there to read. With pragma once, the compiler puts the file name into a list which it looks at before doing the next include - if the file's in there, it won't even be opened. Since opening a file can take quite some time, this can speed up compilation.
Of course, other compilers are way ahead of MS's and simply recognize the include guard pattern. If there is no functional token outside the #ifndef SYMBOL #define SYMBOL ... #endif sequence, the compiler puts the file into the blacklist after including it. Same benefit, but no programmer interaction or non-standard pragma needed.