Click here to Skip to main content
14,971,029 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am porting some old legacy MFC apps in VC6++ and saw many these kinds of guards in the header files:
#if !defined(AFX_MOUSER_H__66D9BBF5_DD8D_11D4_B7C9_0050DAB84384__INCLUDED_)
#define AFX_MOUSER_H__66D9BBF5_DD8D_11D4_B7C9_0050DAB84384__INCLUDED_

// code snippet in between

#endif // !defined(AFX_MOUSER_H__66D9BBF5_DD8D_11D4_B7C9_0050DAB84384__INCLUDED_)

I know it is used by AppWizard to edit code. but now in Visual Studio 2017,
these kinds of tricks as guard statements are not used any more. I validated my understanding by creating a dialog based MFC app using AppWizard.

So is it safe for me to remove all old guards in .h header files?
Any advice and recommendation for doing this?

What I have tried:

did some simple tests on AppWizard generated demo app, it is okay.
Posted
Updated 8-Oct-20 20:11pm

It is the old fashioned way of preventing header files from being included more than once. You can delete those lines and just add the following at the top of the file:
C++
#pragma once
   
v3
Comments
CPallini 9-Oct-20 2:07am
   
5.
As Richard noted, you cannot simply remove them, you have to replace them with the
#pragma once pragma.
To add my two cents to Richard's answer (GCC documentation):

#pragma once
If #pragma once is seen when scanning a header file, that file will never be read again, no matter what. It is a less-portable alternative to using '#ifndef' to guard the contents of header files against multiple inclusions.
   
v2
Comments
Southmountain 9-Oct-20 14:54pm
   
thank you for sharing this GCC tip!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900