#pragma once

Started by
7 comments, last by Barguast 20 years ago
I was under the impression that this was specific to Visual C++. But according to MSDN it isn't. Is this basically the same thing as doing the following?

#ifndef FOO
#define FOO

...

#endif
 
If so, then why does Visual C++ use both methods? [edited by - Barguast on April 13, 2004 6:21:20 PM]
Using Visual C++ 7.0
Advertisement
AFAIK it is not consistent across compilers. .net uses just #pragma once but I would not like to trust to portability on that one.
------------------------See my games programming site at: www.toymaker.info
quote:Original post by Trip99
AFAIK it is not consistent across compilers.


Correct. #pragma once will not work on all compilers.

With #ifndef, #define, and #endif, there might be a very small chance for name collision, where as with #pragma once, there ins''t.

Visual C++ uses both methods because it''s just one of those things that''s in there, along with all the other weird stuff.
I beleive #pragma once will prevent the file from being opened a second time, the regular header guards won''t work without opening the file and possibly including some 40M lines of headers above the guards.

#pragma once works in GCC too I just wish that they could release the version that does...
I read somewhere that VC''s implementation of #pragma once is broken - it can be fooled by including via relative paths. It''s typically only used as an optimization - regular include guards are necessary to guarantee single inclusion. Other compilers are able to detect include guards and keep the information in a table (what #pragma once does in VC, apparently).
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3
quote:Original post by Jan Wassenberg
I read somewhere that VC''s implementation of #pragma once is broken - it can be fooled by including via relative paths. It''s typically only used as an optimization - regular include guards are necessary to guarantee single inclusion. Other compilers are able to detect include guards and keep the information in a table (what #pragma once does in VC, apparently).
Do you have any source of information? I''d be interested in details, since I''ve been using VC''s #pragma once for years in both small and large projects and never seen any problems with it.
If I recall correctly,
#pragma once 
is a deprecated piece of code that shouldn't be used. Sucks, I know... it was a useful little friend. However, it's definitely better to stay standard and not use it


- CD


// Brought to you by Code_Dark
| [( Politics Forum Beta-Test )]


[edited by - Code_Dark on April 13, 2004 7:46:56 PM]
I have only my memory, and a post I just found describing the problem.

This probably won''t happen in most projects, but I sometimes find myself including via (different) relative paths, so it''d blow up with #pragma once only.
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3
quote:Original post by Leffe
#pragma once works in GCC too I just wish that they could release the version that does...


It''s "deprecated" in current versions, and you''ll get warnings if you use it, but they''re bringing it back in GCC 3.4.

This topic is closed to new replies.

Advertisement