Sign in to follow this  
TheOddMan

#pragma once

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by TheOddMan
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?


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 this post


Link to post
Share on other sites
Quote:
Original post by sebarnolds
I 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 this post


Link to post
Share on other sites
Quote:
Original post by CTar
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).

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

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by TheOddMan
is 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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this