Solved

Why "#pragma once"?

Posted on 1998-05-03
3
796 Views
Last Modified: 2013-11-19
MFC generated header files always have the

#if !defined( __MY_HPP )
#define __MY_HPP

#if _MSC_VER >= 1000
#pragma once
#endif

Question :
Why does it still need the "#pragma once" when it already has the
"#if !defined( __MY_HPP )
#define __MY_HPP" ?

0
Comment
Question by:nancytan
3 Comments
 

Author Comment

by:nancytan
ID: 1306572
Edited text of question
0
 
LVL 11

Accepted Solution

by:
mikeblas earned 0 total points
ID: 1306573
1) Because it might be compiled with an older version of the compiler that doesn't support #pragma once.  (See the _MSC_VER ifdef?)

2) #pragma once causes the compiler to immediately stop processing the whole file. The guarded #ifdef's require the preprocessor to continue reading the file line-by-line, looking for the #endif. #pragma once just ends, on the spot.  It's much faster.

.B ekiM
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1306574
Neither reason makes sense here, Mike...

The first doesn't explain why #pragma is there .. only why its use is protected by a #if..#endif check on the compiler version.  Also, of course, the code may be compiled by a non MS compiler (not just an older version).

The second reason is (slightly) flawed also.  The #pragma once doesn't do what you said/implied (ie. stops processing when the #pragma line is found).

According to the docs, it actually doesn't even attempt to OPEN the file on subsequent #includes (let alone read it).  So on a subsequent #include of the .h file, then #include won't happen at all (the .h file is not even opened).  So it is much more efficient - even more that your answer implied.

If the #pragma did work as you had implied, then it would only be processed on the first #include (when __MY_HPP is not defined) and on subsequent #include's it will not even be looked at (just read and skipped while the preprocessor looks for the matching #endif)  So it would actually be redundant and will NEVER have any effect in the code snippet supplied.

BTW: After looking more closely at what #pragma once does, I think I'll go thru my .h files and ensure they all have #pragma once in them .. it should speed up my builds.  Thanks to nacytan and mike for drawing my attention to this.


0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Strange date stored 22 123
Unix / Linux grid computing 5 157
fizzArray  challenge 1 84
twoTwo  challenge 35 103
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question