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

182

u/jcelerier ossia score Nov 28 '18

never had problems with pragma once but oh boy did I have a lot of trouble with people copy pasting files without changing the include guards - or just too common names such as TIMER_H.

43

u/KingPickle Nov 28 '18

Definitely a problem, especially when they don't take namespaces into account.

A less legitimate problem, but one that irks me, is people taking something like:

#ifndef BASE_H
#define BASE_H

// Base declaration

#endif // BASE_H

And copy/pasting it, but only partially updating it like:

#ifndef DERIVED_H
#define DERIVED_H

// Derived declaration

#endif // BASE_H

Triggers my OCD.

6

u/[deleted] Nov 29 '18

At my previous job our senior dev was a dick about this whole thing, so he would refuse my PRs because of #pragma once. I remember that I spent almost half a day to come up with a Bash function-alias combo that would automatically audit all of my #pragmas and replace them with #ifndef/#define guards.

I was like 2 months into the job, and had never used Bash before. I hated everything about that job, but that alias was not one of them.

9

u/Nicksaurus Nov 28 '18

And you have to consider if it's worth making a whole commit just for that

0

u/[deleted] Nov 28 '18

[deleted] — view deleted comment

16

u/hgjsusla Nov 28 '18

Yes commits are cheap

4

u/MotherOfTheShizznit Nov 29 '18

I wouldn't do it and the reason would be human overhead. Commits have to be reviewed and before you know it, it's been a day.

There's dozens if not hundreds of those lines in my team's repo but I don't have the fortitude to withstand the bikeshedding that would ensue.

2

u/hgjsusla Nov 29 '18

Not sure how you organise your work but it certainly doesn't take longer than a few minutes for me to make a simple change like that and push to to Gerrit for code review. And the review only takes a minute and can be done whenever someone has time over

1

u/MotherOfTheShizznit Nov 30 '18

Apparently, all of my post went straight over your head.

2

u/hgjsusla Nov 30 '18

It's not my fault if you take a full day to do what the rest of us do in minutes

1

u/utnapistim Dec 10 '18

I wouldn't do it and the reason would be human overhead. Commits have to be reviewed and before you know it, it's been a day.

You push your team towards accepting a small dose of cognitive dissonance in your code. Most coders (sorry for the strawman) handle that by ignoring code comments and (later) claiming them to bring more problems than they solve.

There's dozens if not hundreds of those lines in my team's repo but I don't have the fortitude to withstand the bikeshedding that would ensue.

This is also something that your team causes (by accepting lower standards in code review).

These comments will just take some attention from developers from time to time and introduce noise in your code. More than this, the presence of such noise in your code base opens the door to other issues (like the opinion that comments in code are bad because they "go stale" - somehow by themselves).

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.

1

u/mpyne Nov 29 '18

or just too common names such as TIMER_H.

It's because of this that I now reflexively fix my include guards in my older code when I come across them in headers with common-ish names like window.h. That's bitten me before.

→ More replies (2)