Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reason for #pragma once inside header guards?

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?

like image 761
dubnde Avatar asked Aug 13 '10 09:08

dubnde


2 Answers

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

like image 108
Aoi Karasu Avatar answered Nov 13 '22 09:11

Aoi Karasu


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

like image 43
DumbCoder Avatar answered Nov 13 '22 08:11

DumbCoder