3

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

Tamás Szelei
  • 23,169
  • 18
  • 105
  • 180
aCuria
  • 6,935
  • 14
  • 53
  • 89

4 Answers4

3

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

Xeo
  • 129,499
  • 52
  • 291
  • 397
3

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.

Dennis Zickefoose
  • 10,791
  • 3
  • 29
  • 38
1

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.

joce
  • 9,624
  • 19
  • 56
  • 74
1

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.

Christian Rau
  • 45,360
  • 10
  • 108
  • 185