Do I understand this right, why #pragma once is useful?
Its because if you have two files included in a cpp/h, that both in their source code have included another header? Both the headers are the same so the definition is included twice, which is not allowed in c++ so you use #pragma once in the header file to tell the compiler to not include this header twice?
I personally think this should be included in all cpp files. It should never include stuff twice because thats a waste. Is there any situation in which you would need to include something twice?
You mean that the preprocessor that actually concatenates the code of a compilation unit for the compiler should automatically keep track of each file that it has included (and ignore second appearances). That is what the pragma effectively does. The #ifdef inclusion guards require no special bookkeeping from the preprocessor (except the list of defined macros).
How many preprocessor implementations are there? If you write a library for others, can you trust that all of them use a preprocessor that supports your "new way"TM?
How would you allow double inclusion, if someone would need it?
(1) portability - it is nonstandard, and hence there do still exist important implementations that don't implement it
(2) file system errors - it is easily possible to create distinct file paths that actually reference the same file. Few systems (even modern) can actually resolve this easily, and compiler systems typically won't even bother to try.
That is why the so-called "include guards" are still the safest option. IIRC, some compilers recognize the pattern and apply the same logic to the files as if #pragma were used. Should it fail the include guards continue to provide safety.
There is no reason you cannot combine the two anyway. This is the most useful across the greatest number of compiler systems.
1 2 3 4 5 6 7 8 9 10 11
Library that does cool but useless stuff
Copyright 3012 John Jacob Jingleheimer Schmidt
constchar* foo( constchar* s );