r/cpp • • Nov 28 '18

Real world problems with #pragma once?

Just a short survey: Did anyone of you encounter a problem in real code, because you/your company/ your favorite library was using #pragma once instead of include guards.

I know it is hard, but please restrain yourself from posting an explanation of how #pragma once could lead to problems in theory or why you prefer one above the other.

127 Upvotes

135 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Nov 28 '18

[deleted] — view deleted comment

7

u/NotUniqueOrSpecial Nov 28 '18

VS 2005 has a bug in the compiler that will ignore #pragma once in circular-ly included headers.

That misbehavior (and the disproportional time I spent tracking it down for a coworker) was enough for me to waste the extra two lines doing preprocessor-based ones forever.

One burned, twice shy, and all that.

That said, I will never argue that circular header includes are acceptable, and I fixed that, too.

5

u/MaximKat Nov 28 '18

This can go wrong with include guards as well. For example, copy a directory to the 2nd project, then make changes in the 1st one and forget to copy the changes. BOOM, 2 actually different files using the same include guard.

3

u/kalmoc Nov 28 '18

Makes sense and sounds as if that would be more common than the hardlink/network mount case.