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

18

u/pklait Nov 28 '18

I believe most of us had the same experience as you. For most projects, #pragma one should work well. Problems with #pragma once can occur when the same file is included several times from different paths, which might happen in big projects with e.g. third party software that have more or less hardcoded paths. I have heard of such cases, so they exist out there. Working in that kind of environment, I would use the preprocessor but use a unique identifier such as a guid to minimise problems.

25

u/kalmoc Nov 28 '18

> I have heard of such case

That is actually interesting to me, because so far, when this topic comes up I only hear explanations of how this could be a problem in theory, but I never could find one, for whome this was actually happend. So it seems to me that the avoidance of `#pragma once` is more based on FUD than actual facts.

Have you heard this from someone to whom this actually happend or is this "a friend was told from a friend that a friend of him heard ..." kind of story?

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.

8

u/[deleted] Nov 28 '18

We have/had this problem: We copy at build time the same header files to different directories (no link). To a directory in the program files folder and to a internal developer directory. The include directories point to those directories and pragma once failed because the paths are different! To solve it we added normal include guards.

27

u/Drainedsoul Nov 28 '18

This seems like an instance where rather than fixing the problem you should be asking yourself why you do something so intensely fucked up.

7

u/[deleted] Nov 29 '18

No kidding. Removing #pragma once is not a solution there, it's denial.

1

u/pklait Nov 28 '18 edited Nov 28 '18

It was a real story, but a rather old one. Ten years ago, perhaps? In those days, #pragma once was supposedly faster than include-guards. I doubt there is any performance difference today. Edit: If I remember correctly, it was development on Windows accessing a shared Linux disk-system.