Solved

Why "#pragma once"?

Posted on 1998-05-03
3
792 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
Comment Utility
Edited text of question
0
 
LVL 11

Accepted Solution

by:
mikeblas earned 0 total points
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now