Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Why "#pragma once"?

Posted on 1998-05-03
3
Medium Priority
?
816 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month13 days, 2 hours left to enroll

971 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