Reputation: 43
I have researched it online, someone said they are avoid to include the same file more then one time, but i still not clear enough the difference and the meaning of same file.
Thank you advance.
Upvotes: 3
Views: 3698
Reputation: 70546
In theory, #pragma once
is platform specific and #ifndef
style include guards are the only Standard compliant way to ensure header uniqueness through the preprocessor.
However, in practice, all major compilers (gcc, Clang, Visual C++, Intel) support it on all major platforms (Linux, Windows, MacOSX). Maybe some older or obscure compiler/platform combinations give you trouble, but in practice it just works. But caveat emptor!
The major advantage of using #pragma once
is that it is a lot easier to do refactorings, since you don't have to either keep all the include guard names in sync with the file system (one common style guide obligation of defining header guards) or to use a pseudo-random string ID as include guard.
Upvotes: 2
Reputation: 4096
#pragma once
is not part of the standard and may not work on all operating systems or compilers
#ifndef
is the preferred method of only including a file only once on most OS
Other than that #pragma once
is less prone to making mistakes that can be caused by forgetting to change header guards if you copy and paste code etc and it is less code to type.
Upvotes: 4
Reputation: 172528
You can use #pragma once
when you are addressing a specific compiler, #pragma once
is non-standards and is specific to some specific C compilers whereas #ifndef/#define/#endif works on almost every C compiler. #pragma once reduces possibilities for bugs.
From wiki:
In the C and C++ programming languages, #pragma once is a non-standard but widely supported preprocessor directive designed to cause the current source file to be included only once in a single compilation. Thus, #pragma once serves the same purpose as #include guards, but with several advantages, including: less code, avoidance of name clashes, and sometimes improved compile speed.1
Upvotes: 1