Proper usage of #pragma once
-
I've never gotten a good answer out of anyone when I have inquired about the proper way to ensure that a file is included only once in a build. I also did not see anything in my thread search so I'll ask and hopefully get a reasonable answer. In the below subset of code, I though the first set of instructions was to ensure that the header file would be included only once in a build. Then I thought, well what is the pragma once for then? According to the MSDN help, pragma once does the same thing. So when class wizards generate a class header file, why do they insert code that seems to serve duplicate purposes? Any ideas? #if !defined(AFX_RETRYDLG_H__4A3C71B3_E316_48A5_B38E_7F3E778D47DD__INCLUDED_) #define AFX_RETRYDLG_H__4A3C71B3_E316_48A5_B38E_7F3E778D47DD__INCLUDED_ //I've already used the previous #ifndef ... #endif to //ensure the single inclusion of my class declaration. //Why do I need this #pragma once on top of that? #if _MSC_VER > 1000 #pragma once #endif //of course my class would be declared after the #pragma once statement. class MyClass { MyClass(); ~MyClass(); }; #endif // _MSC_VER > 1000
-
I've never gotten a good answer out of anyone when I have inquired about the proper way to ensure that a file is included only once in a build. I also did not see anything in my thread search so I'll ask and hopefully get a reasonable answer. In the below subset of code, I though the first set of instructions was to ensure that the header file would be included only once in a build. Then I thought, well what is the pragma once for then? According to the MSDN help, pragma once does the same thing. So when class wizards generate a class header file, why do they insert code that seems to serve duplicate purposes? Any ideas? #if !defined(AFX_RETRYDLG_H__4A3C71B3_E316_48A5_B38E_7F3E778D47DD__INCLUDED_) #define AFX_RETRYDLG_H__4A3C71B3_E316_48A5_B38E_7F3E778D47DD__INCLUDED_ //I've already used the previous #ifndef ... #endif to //ensure the single inclusion of my class declaration. //Why do I need this #pragma once on top of that? #if _MSC_VER > 1000 #pragma once #endif //of course my class would be declared after the #pragma once statement. class MyClass { MyClass(); ~MyClass(); }; #endif // _MSC_VER > 1000
#pragma once is a microsoft only pragma, and in the example you show, it is useless ( i think ). I usually remove it from generated code.
Maximilien Lincourt Your Head A Splode - Strong Bad
-
I've never gotten a good answer out of anyone when I have inquired about the proper way to ensure that a file is included only once in a build. I also did not see anything in my thread search so I'll ask and hopefully get a reasonable answer. In the below subset of code, I though the first set of instructions was to ensure that the header file would be included only once in a build. Then I thought, well what is the pragma once for then? According to the MSDN help, pragma once does the same thing. So when class wizards generate a class header file, why do they insert code that seems to serve duplicate purposes? Any ideas? #if !defined(AFX_RETRYDLG_H__4A3C71B3_E316_48A5_B38E_7F3E778D47DD__INCLUDED_) #define AFX_RETRYDLG_H__4A3C71B3_E316_48A5_B38E_7F3E778D47DD__INCLUDED_ //I've already used the previous #ifndef ... #endif to //ensure the single inclusion of my class declaration. //Why do I need this #pragma once on top of that? #if _MSC_VER > 1000 #pragma once #endif //of course my class would be declared after the #pragma once statement. class MyClass { MyClass(); ~MyClass(); }; #endif // _MSC_VER > 1000
The way i understand it (could be wrong) is that the #ifndef test prevents referential loops when the compiler is recursing through the #include lists for a given module. This is on a per module (cpp file) basis. Where as the #pragma once is for the entire build session. It likely instructs the compiler to cache the parsed contents of the file so that the second module that needs the file doesn't have to reparse it. ...cmk Save the whales - collect the whole set
-
I've never gotten a good answer out of anyone when I have inquired about the proper way to ensure that a file is included only once in a build. I also did not see anything in my thread search so I'll ask and hopefully get a reasonable answer. In the below subset of code, I though the first set of instructions was to ensure that the header file would be included only once in a build. Then I thought, well what is the pragma once for then? According to the MSDN help, pragma once does the same thing. So when class wizards generate a class header file, why do they insert code that seems to serve duplicate purposes? Any ideas? #if !defined(AFX_RETRYDLG_H__4A3C71B3_E316_48A5_B38E_7F3E778D47DD__INCLUDED_) #define AFX_RETRYDLG_H__4A3C71B3_E316_48A5_B38E_7F3E778D47DD__INCLUDED_ //I've already used the previous #ifndef ... #endif to //ensure the single inclusion of my class declaration. //Why do I need this #pragma once on top of that? #if _MSC_VER > 1000 #pragma once #endif //of course my class would be declared after the #pragma once statement. class MyClass { MyClass(); ~MyClass(); }; #endif // _MSC_VER > 1000
Personally, I'd stay with the #if, etc. directives as this is a more portable way of doing things. However, the reason for...
#if _MSC_VER > 1000 #pragma once #endif
...appearing is (IIRC) due to a bug with the developer studio debugger when keeping track of something or another. I tried googling to refresh my memory to no avail though. Jeremy Falcon -
I've never gotten a good answer out of anyone when I have inquired about the proper way to ensure that a file is included only once in a build. I also did not see anything in my thread search so I'll ask and hopefully get a reasonable answer. In the below subset of code, I though the first set of instructions was to ensure that the header file would be included only once in a build. Then I thought, well what is the pragma once for then? According to the MSDN help, pragma once does the same thing. So when class wizards generate a class header file, why do they insert code that seems to serve duplicate purposes? Any ideas? #if !defined(AFX_RETRYDLG_H__4A3C71B3_E316_48A5_B38E_7F3E778D47DD__INCLUDED_) #define AFX_RETRYDLG_H__4A3C71B3_E316_48A5_B38E_7F3E778D47DD__INCLUDED_ //I've already used the previous #ifndef ... #endif to //ensure the single inclusion of my class declaration. //Why do I need this #pragma once on top of that? #if _MSC_VER > 1000 #pragma once #endif //of course my class would be declared after the #pragma once statement. class MyClass { MyClass(); ~MyClass(); }; #endif // _MSC_VER > 1000
When you use the
#pragma once
directive, the compiler will only open and read the file only once. If all you use is the#ifdef...#endif
code block, then the compiler will open and read the file every time it is encountered, which is not all that efficient when all of the files contents gets ignored anyway. The only think to remember is that the#pragma once
does not work on older compilers, so it is wise to always include both methods in your header file.
[
](http://www.canucks.com)"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!
-
I've never gotten a good answer out of anyone when I have inquired about the proper way to ensure that a file is included only once in a build. I also did not see anything in my thread search so I'll ask and hopefully get a reasonable answer. In the below subset of code, I though the first set of instructions was to ensure that the header file would be included only once in a build. Then I thought, well what is the pragma once for then? According to the MSDN help, pragma once does the same thing. So when class wizards generate a class header file, why do they insert code that seems to serve duplicate purposes? Any ideas? #if !defined(AFX_RETRYDLG_H__4A3C71B3_E316_48A5_B38E_7F3E778D47DD__INCLUDED_) #define AFX_RETRYDLG_H__4A3C71B3_E316_48A5_B38E_7F3E778D47DD__INCLUDED_ //I've already used the previous #ifndef ... #endif to //ensure the single inclusion of my class declaration. //Why do I need this #pragma once on top of that? #if _MSC_VER > 1000 #pragma once #endif //of course my class would be declared after the #pragma once statement. class MyClass { MyClass(); ~MyClass(); }; #endif // _MSC_VER > 1000
One benefit not yet mentioned is that if you use the
#ifdef
method, you can test later to see if the header was included. For example WTL has this:#ifndef __ATLBASE_H__
#error atlapp.h requires atlbase.h to be included first
#endifto check that atlbase.h is included before atlapp.h. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Four fonts walk into a bar. The bartender says "Hey - get out! We don't want your type in here."
-
When you use the
#pragma once
directive, the compiler will only open and read the file only once. If all you use is the#ifdef...#endif
code block, then the compiler will open and read the file every time it is encountered, which is not all that efficient when all of the files contents gets ignored anyway. The only think to remember is that the#pragma once
does not work on older compilers, so it is wise to always include both methods in your header file.
[
](http://www.canucks.com)"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!
thanks for the additional info, I think I read that before, but it's nice to have a reminder. :-D
Maximilien Lincourt Your Head A Splode - Strong Bad