#pragma once

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

Recommended Posts

My question concerns the following piece of code.
#if _MSC_VER > 1000
#pragma once
#endif


From what I understand, using #pragma once makes the compiler only compile the header file once. This seems like it should be the normal behaviour, is there any reason I wouldn't want to use this in a header file?

Share on other sites
Quote:
 This seems like it should be the normal behaviour,

What do you mean with normal behaviour? You mean the coder should only include it once? If you do then it can easily be done for small projects, but bigger projects have lots of nested header files.

Imagine you have A.hpp, B.hpp and C.hpp

A.hpp contains the class A, B.hpp contains the class B and C.hpp contains the class C.

C is dependent on both A and B so in C we have:

#include "A.hpp"#include "B.hpp"

A is dependent on B so in A.hpp we got:
#include "B.hpp"

So when the first #include line is parsed we have:

C.hpp
// The following two lines are the content of A.hpp#include "B.hpp"// Here A's implementation is#include "B.hpp"// C's implementation

So we got two B includes which will cause errors. You can make code without the #pragma once, but you'll most likely have 30+ includes in each file and when a file is added, deleted or dependices are added or deleted you will have to use hours updating the #includes in all files.

Share on other sites
I think what the OP is asking is whether you would ever _want_ a header to be included more than once in a single translation unit.

For headers containing class definitions, function prototypes etc then I can't see why you would.

Share on other sites
Yeah that's right Bakery, I can't see any reason why you shouldn't use #pragma once. Does anyone know any reason you wouldn't?

Share on other sites
I remember receiving warnings from gcc telling me that "pragma once" was deprecated. I don't know why. MS compiler doesn't complain.

Now, I use the following in my headers :

#ifndef BLABLA_HEADER#define BLABLA_HEADER...#endif

Share on other sites
Quote:
 Original post by TheOddManYeah that's right Bakery, I can't see any reason why you shouldn't use #pragma once. Does anyone know any reason you wouldn't?

In my current project some of the classes generates warnings because of missing copy constructors, assignment operators, unreferenced parameters in release mode etc. To fix this I need to disable warnings, the best way to to this is to disable them before the class and enable the warnings after the class. The problem though is that if I needed to disable warnings for all compilers I can think of it would be a mess and I would have to update all headers when adding support for a new compiler.

Therefore I have created some files so I can just do like this:

#include "M1ABase/Warnings/Push.hpp"#include "M1ABase/Warnings/NoAssignmentOperator.hpp"#include "M1ABase/Warnings/NoCopyConstructor.hpp"class A{};#include "M1ABase/Warnings/Pop.hpp"

First I push a new "warning stack", then I disable the warnings for "no assignment operator" and "no copy constructor", and after the class I pop of the stack and the warnings are like before.

There is a good chance these warning headers need to be included more than once. This is the only case I have seen where it shouldn't be used.

I also have some header in which I don't use #pragma once because I know the headers are only going to included by one file which have #pragma once in it.

Share on other sites
Quote:
 Original post by sebarnoldsI remember receiving warnings from gcc telling me that "pragma once" was deprecated. I don't know why. MS compiler doesn't complain.

That is because #pragma once is a compiler specific statement, it does the same thing as the inclusion guard you posted, but with less code and often faster.

It is faster because if #pragma once is in file A.hpp then the compiler shouldn't open A.hpp again, also since A.hpp isn't opened all the other times it doesn't have to be parsed by the preprocessor either (the preprocessor needs to find the #endif so it knows there is nothing beyound it).

Share on other sites
Quote:
 Original post by CTarIt is faster because if #pragma once is in file A.hpp then the compiler shouldn't open A.hpp again, also since A.hpp isn't opened all the other times it doesn't have to be parsed by the preprocessor either (the preprocessor needs to find the #endif so it knows there is nothing beyound it).

GCC provides this optimization using #ifdef blocks as well. So on GCC, there's really no reason to use #pragma once.

CM

Share on other sites
Quote:
 Original post by TheOddManis there any reason I wouldn't want to use this in a header file?

Two reasons:
a) It's compiler specific, some won't use it. Inclusion guards are better if you want compiler compatibility.

b) You don't want it when you have specific reasons to include a header multiple times like with boost::preprocessor.

Share on other sites
I believe the reason to avoid #pragma once and instead appeal to inclusion guards is because the #pragma preprocessor directives are all compiler specific, and so if you come across the situation of having to recompile, you may be so unfortunate so as to have a compiler which doesn't recognize #pragma once.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• Forum Statistics

• Total Topics
631431
• Total Posts
3000039
×