Is #Pragma Once a Safe Include Guard

Is #pragma once a safe include guard?

Using #pragma once should work on any modern compiler, but I don't see any reason not to use a standard #ifndef include guard. It works just fine. The one caveat is that GCC didn't support #pragma once before version 3.4.

I also found that, at least on GCC, it recognizes the standard #ifndef include guard and optimizes it, so it shouldn't be much slower than #pragma once.

#pragma once vs include guards?

I don't think it will make a significant difference in compile time but #pragma once is very well supported across compilers but not actually part of the standard. The preprocessor may be a little faster with it as it is more simple to understand your exact intent.

#pragma once is less prone to making mistakes and it is less code to type.

To speed up compile time more just forward declare instead of including in .h files when you can.

I prefer to use #pragma once.

See this wikipedia article about the possibility of using both.

Why using both #pragma once and include guard?

Actually there might be a small difference inside the compiler. When compiler encounters #pragma once then it can internally mark that this file is included. When it encounters #include for this file for the second time it won't bother even to open it, it will just ignore the #include statement.

With only include guard the preprocessor has to parse the whole file every time it's included to find a matching #endif. Theoretically, if really complicated and large include files are included multiple times this can affect compilation time.

Other than that, include guard and #pragma once behave the same way. Both of them are usually used because #pragma once is not guaranteed to be supported on all compilers.

Edit:

Compiler might have the ability to detect that include guard's statements are surrounding the whole file's code and deduce that it's an include guard, producing exactly the same behavior as with #pragma once. If that's the case then MSDN's claim is correct.

When to use include guards or #pragma once C++

Summarizing the comment by Galik and what I realized:

Include guards should be put in every header file in the case that something in the future conflicts. Furthermore, the small time it takes the compiler to process the include guards will make the compilation faster since the extra header does not need to be processed.

What are the dangers of using #pragma once?

One problem I have encountered with using #pragma once was when including the same file that is located at multiple locations. With #pragma once it is deemed different, not with #ifndef/#define guard.

Should I still use #include guards AND #pragma once?

It depends on how much portable your program is expected to be.

As long as you are writing a program which is supposed to work with compilers which you know definitely support #prama once, just using #pragma once should suffice. But doing so you restrict your program to set of compilers which support the implementation defined feature.

If you need your program to work on all compilers then you should use #pragma once and include guards both.

In case a compiler does not support #pragma once it will simply ignore it[Ref#1], in such a case the header guards will serve you the purpose, so nothing wrong in using them both when you are not aware of features supported by your target compilers.

So if you want your program to be 100% portable on different compilers the ideal way is still to use only the include guards. As @CharlesBailey rightly points out since the behavior for #pragma once is implementation defined, the behavior on an unknown compiler might have a detrimental effect on your program.


[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.

C++ redefinition with include guard and #pragma once

Technically, you do not need to use #pragma once and #ifndef.. #define... #endif together. They are the same algorithms for compiling the header file only once!
I never encountered the problem myself, but for the purpose of multiplatforming and compiling without errors - I would use the latter (#ifndef.. and #endif) only.

About the header, it is written good. The purpose of the header is to declare the class variables and member functions. And, of the .cpp is to define. That is why they are called declaration and definition files, respectively.
And it seems that you did it right. Only make sure to include only the .hpp in your main.cpp, to prevent the redefinition error.

EDIT: Note, if it is of any help, that C++ accepted header files are: .h, .hpp, .hh
and definition types are: .C, .cc, .cpp.



Related Topics



Leave a reply



Submit