Do I understand this?

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've got it right, I can't think of any situations where you would want to include something more than once. However #pragma once generally gets a bad rap for not being portable, many people chose to use #ifdef header guards. http://faculty.cs.niu.edu/~mcmahon/CS241/c241man/node90.html
I personally think this should be included in all cpp files.


Since one shouldn't #include cpp files, that would be pointless.
I personally think this should be included in all

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?
Why would you need it twice kesiverto?
Last edited on
closed account (z05DSL3A)
There are times when including a header file multiple times can be advantageous, giving you different options/behavior depending on what is #defined at the time of including.
Last edited on
Technically it's non-standard.
@Anmol444: Principle. Denying anything because "nobody needs it" is evil.
Ok thanks guys!
Most modern CPPs understand #pragma once .

However, there remain two important issues:

(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
/* foo.h
    Library that does cool but useless stuff
    Copyright 3012 John Jacob Jingleheimer Schmidt
*/
#pragma once
#ifndef SCHMIDT__FOO_H
#define SCHMIDT__FOO_H

const char* foo( const char* s );

#endif 

Hope this helps.
Topic archived. No new replies allowed.