Jump to content
  • Advertisement
Sign in to follow this  
alnite

how safe is using #pragma once

This topic is 4963 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I hate defining _HEADER_NAME_H_ for every header file. #pragma once IMO is much cleaner. It used to be MSVC++ specific, and I heard gcc has implemented it too. I am not sure with the other compilers. So, can it now be considered as a standard preprocessor command?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by alnite
I hate defining _HEADER_NAME_H_ for every header file.

17.4.3.1.2:
Each name that contains a double underscore (__) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the [compiler implementors] for any use.
Quote:

#pragma once IMO is much cleaner. It used to be MSVC++ specific, and I heard gcc has implemented it too. I am not sure with the other compilers.

So, can it now be considered as a standard preprocessor command?

No. However, if those are the only compilers you are going to target, feel free to use #pragma once

Share this post


Link to post
Share on other sites
No, it can't ben considered a standard command just because one or two compilers support it. For example, if you went to use your code with SN Systems compiler for PS2, it probably won't work. Same thing Metroworks compiler for various console systems.

If you have no desire to move your code base to anything other than Windows then do what you want. But, I wouldn't count on it working everywhere.

Personally, I try to use the least amount of platform specific stuff simply so that it's easier to re-use no matter when/where I want to use it. Each to their own though...

-John

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by alnite
I hate defining _HEADER_NAME_H_ for every header file. #pragma once IMO is much cleaner. It used to be MSVC++ specific, and I heard gcc has implemented it too. I am not sure with the other compilers.


Pretty much everyone uses include guards. Clean or not, nobody will be confused by them, and nobody will fault you for using them.

Quote:

So, can it now be considered as a standard preprocessor command?


No, a pragma is per definition not standard, but the standard allows use of any pragmas, as compilers are supposed to ignore pragmas they don't understand. Herein lies the problem... If you have used #pragma once instead of include guards, the whole semantic of the program has changed. A compiler that has no #pragma once will ignore it, and there will be no inclusion guards in place. Your program will essentially be broken C++. It's up to you if you want to be that anal or not. For the code to be valid C++, use either include guards alone or #pragma once along with them.

I also have a vague recollection that #pragma once also does not guarantee that a file will not be included more than once, just that it will only be read once.

Share this post


Link to post
Share on other sites
This issue is a matter of opinion, and here's my opinion: Since it's really very rare that you will switch compilers during a project, I say just use #pragma once. It definitely is cleaner. (and it's easy to fix in that rare situation where you do switch compilers.)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Depends on your definition of clean. IMHO pragmas are about the only thing that are dirtier than regular preprocessor options.

Share this post


Link to post
Share on other sites
Quote:
Original post by pinacolada
This issue is a matter of opinion, and here's my opinion: Since it's really very rare that you will switch compilers during a project, I say just use #pragma once. It definitely is cleaner. (and it's easy to fix in that rare situation where you do switch compilers.)

Yes, your co-workers will thank you for your forsight on using #pragma once when they have to go through and change all of your silly headers.

Share this post


Link to post
Share on other sites
Quote:
Original post by pinacolada
This issue is a matter of opinion, and here's my opinion: Since it's really very rare that you will switch compilers during a project, I say just use #pragma once. It definitely is cleaner. (and it's easy to fix in that rare situation where you do switch compilers.)

Y'know what'd be easier to fix? Using include guards! Do you know why? Because there'd be nothing to fix!

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
Quote:
Original post by pinacolada
This issue is a matter of opinion, and here's my opinion: Since it's really very rare that you will switch compilers during a project, I say just use #pragma once. It definitely is cleaner. (and it's easy to fix in that rare situation where you do switch compilers.)

Yes, your co-workers will thank you for your forsight on using #pragma once when they have to go through and change all of your silly headers.


Oh yeah, cause we're *definitely* going to migrate to a non-MS compiler in the future. We only just upgraded from VC6 to VC.net two weeks ago.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
And if your company decides to sell a source code license to someone, who perhaps uses borland or something else? Will they be pleased?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!