aCuria
aCuria

Reputation: 7215

Why are header guards unnecessary in stdafx.h?

I don't understand why header guards are not used in pre-compiled headers... Any ideas?

Upvotes: 4

Views: 1390

Answers (4)

Dennis Zickefoose
Dennis Zickefoose

Reputation: 10979

If all you do is include other headers, there's no need. If those files can not be included multiple times, they will have their own header guards. stdafx.h itself doesn't care how many times it is included unless you're using it wrong.

Upvotes: 3

Christian Rau
Christian Rau

Reputation: 45968

I do not know the code of the precompiled header, but I guess it contains a "#pragma once", which has the same effect but only works in VS (at least it's not standard). I remember wizard created MFC files using these.

Upvotes: 1

joce
joce

Reputation: 9902

Usually, stdafx.h will be included only once per cpp file, as the first statement, and normally, no other files will include it. So, chances of recursively including stdafx.h are minimal, thus the "unnecessariness" of the include guard.

I would still advise to use one, just in case, or potentially use #pragma once at the top of the file.

Upvotes: 1

Xeo
Xeo

Reputation: 131887

Because "stdafx.h" has to be the first include in .cpp files, not anywhere else.

Upvotes: 4

Related Questions