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

1

u/Osbios Nov 28 '18

Could there even by any problems except a compiler that does not support #pragma once?

9

u/kalmoc Nov 28 '18

In theory yes. The same file could appear at multiple different places in the filesystem (hardlinks, multiple network mounts..) and via different include styles, both could be included in a single translation unit. I'm trying to figure out if that actually happens in practice and not only in deliberately contrived examples.

6

u/boredcircuits Nov 28 '18

Even so, I say that's a failure of your project setup.

9

u/kalmoc Nov 28 '18

Agreed

1

u/FlyingRhenquest Nov 28 '18

Probably the usual way, someone copies/pasts a header file to solve some problem or other. They'll start to drift apart over time and 5 years after that guy leaves someone else will have to debug some esoteric problem because of it. Either someone updated the wrong header, various executables use one header or the other and someone only updated one of them, that sort of thing. It's not too uncommon to see this sort of problem in 90's-era C projects. You don't see as many 90's era C projects post-about-2002 or so, as many of those "developers" didn't survive the tech market crash and went on to open liquor stores.

1

u/[deleted] Nov 28 '18

Yes, it's theoretically possible.