Solved

Why "#pragma once"?

Posted on 1998-05-03
3
806 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Suggested Courses

623 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