All of my header files use include guards as well as pragma once:
#pragma once
#ifndef FILE_NAME_H
#define FILE_NAME_H
class foo
{
//foo interface..
};
#endif /* FILE_NAME_H */
I understand that pragma once is not standard and may not be the same across compilers, but is there any chance it will cause and error? Would it be better to somehow test if it's available first?
#ifdef THIS_COMPILER_SUPPORTS_PRAGMA_ONCE
#pragma once
#endif
#ifndef FILE_NAME_H
#define FILE_NAME_H
class foo
{
//foo interface..
};
#endif /* FILE_NAME_H */
I want to provide pragma once as an option to possibly speed-up compilation and avoid name-clashing, while still providing compatibility across compilers.
present tense third-person singular of do.
“Does” is used for singular subjects like “he,” “she,” “it,” “this,” “that,” or “John.” “Do” is used to form imperative sentences, or commands. Example: Do your homework. “Does” is never used to form imperative sentences.
Do is an irregular verb. Its three forms are do, did, done. The present simple third person singular is does: Will you do a job for me?
A DOS, or disk operating system, is an operating system that runs from a disk drive. The term can also refer to a particular family of disk operating systems, most commonly MS-DOS, an acronym for Microsoft DOS.
If #pragma once
is not supported it will simply be ignored[Ref#1] and header guards will serve you the purpose, so nothing wrong in using them both, you don't really need any check for the support of #pragma once
.
So the ideal way is to use both #pragma once
and include guards and you have a portable code that can also take advantage of #pragma once
optimization's the compiler may support.
[Ref#1]
Standard C++03: 16.6 Pragma directive
A preprocessing directive of the form
# pragma pp-tokensopt new-line
causes the implementation to behave in an implementation-defined manner. Any pragma that is not recognized by the implementation is ignored.
The standard says "Any pragma that is not recognized by the implementation is ignored.", so you're probably fine, even if a compiler doesn't know #pragma once.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With