how safe is using #pragma once

Started by
24 comments, last by dalleboy 19 years, 5 months ago
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?
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

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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
- John
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.
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.)
Depends on your definition of clean. IMHO pragmas are about the only thing that are dirtier than regular preprocessor options.
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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!
CoV
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.
And if your company decides to sell a source code license to someone, who perhaps uses borland or something else? Will they be pleased?

This topic is closed to new replies.

Advertisement