Solved

Why "#pragma once"?

Posted on 1998-05-03
3
799 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 49
Looking for a specific application/software 2 116
windows 10 how make picture as desktop background 2 65
either24  challenge 19 101
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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