views:

127

answers:

6

Just seen this inside <boost/asio.hpp>

#ifndef BOOST_ASIO_HPP
#define BOOST_ASIO_HPP

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)

/// ....

#endif // BOOST_ASIO_HPP

Disregarding the _MSC_VER preprocessor checks, what is the benefit of having the #pragma once in this case? Doesn't the preprocessor header guard ensure in all cases and on all platforms, the header contents are only ever included once?

A: 

Yes header guards ensures that the header contents are included only once. but here you are using #pragma for checking another definition and not include file.

The below link is existing question on header guards in SO.

http://stackoverflow.com/questions/2979384/purpose-of-header-guards

ckv
The header guard does this anyway, right? why need #pragma once as well?
MeThinks
The header guard tells the preprocessor that it must silent all lines in the file from the `#ifndef` to the `#endif`, but the preprocessor is assumed to read the whole file, and just output no result for the block. The `#pragma once` tells the preprocessor that any later `#include` that contains the same file is to be ignored. The difference is that in the latter case the preprocessor will not even open the file and check. That being said, `#pragma once` is not standard, and compilers have grown to understand the include guard pattern and there should be not much of a difference.
David Rodríguez - dribeas
@MeThinks - maybe not all implementations support this pragma, so you still want to prevent your header from being processed multiple times. The pragma is an added benefit here. (comment duplicated from another answer)
ysap
+1  A: 

Specifies that the file will be included (opened) only once by the compiler in a build. This can reduce build times as the compiler will not open and read the file after the first #include of the module

And one more related question from SO

DumbCoder
Why the down vote ?
DumbCoder
ok. I understand that purpose of header guards. Question is why we have header guard and pragma one
MeThinks
@MeThinks - maybe not all implementations support this pragma, so you still want to prevent your header from being processed multiple times. The pragma is an added benefit here.
ysap
A lot of it is answered in the SO link I gave in the answer.
DumbCoder
+10  A: 

#pragma once specifies that the file will be included (opened) only once by the compiler when compiling a source code file. This can reduce build times as the compiler will not open and read the file after the first #include of the module.

If you don't #pragma once, the file will be opened each time it is needed and compiler will stop parsing it on #ifndef BOOST_ASIO_HPP, if it has been defined.

AOI Karasu
You saying the header is opened and read just the one time if we have #pragma? Header guards means file is always opened but contents within guards skipped, right? Is my understanding correct? If only I could demonstrate this so I see it happen :)
MeThinks
In case of #pragma once it is not always opened. This directive makes the compiler remember not to open it again (more than once). That's why this directive is superior to #ifndef, which could be omitted. However many people still use #ifndef to support old compilers that do not recognize #pragma once.
AOI Karasu
A: 

#pragma once has the same purpose, but include guards are intended to require a deeper analysis to ensure a file is included exactly once - e.g.

// somerandomfileinmyproject.cpp
#undef BOOST_ASIO_HPP 
#include <bost/asio.cpp>

Unless the compiler does handle such cases correctly, it still needs to open the file and pass it through the preprocessor even though it has been included before.

peterchen
A: 

You can reproduce the effect of the #pragma once in a standard way using the following:

#if !defined GUARD_SYMBOL
#include "GUARDED_FILE"
#endif

although it is much more verbose. As others have said, it helps with compilation times since the file is not searched for / opened instead of opening the file and ignoring everything inside it - the file still has to be parsed by the preprocessor.

Skizz
As David noted above, most modern compilers (esp. gcc) understands the header guard pattern well enough to skip the reparsing part, and act as if there was a pragma once or as if the code was written the way you show here.This, and the fact that pragma once cannot correctly handle some corner cases is also the reason why pragma once has been actively rejected from beeing introduced into the new C++0x standard. see also this [SO disscussion](http://stackoverflow.com/questions/787533/is-pragma-once-a-safe-include-guard)
Fabio Fracassi
+1  A: 

However it is not a C++ standard compliant way.

Chubsdad