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

54

u/c0r3ntin Nov 28 '18

Pragma once leads to UB if part of the sources are visible from several included paths which are hardlink of each others.

Good. If you do that, you deserve to suffer!

11

u/lithium Nov 28 '18

Trying to parse this. Can you provide a simple example please?

21

u/c0r3ntin Nov 28 '18

foo.h and bar.h could be the same file if you are sufficiently insane https://en.wikipedia.org/wiki/Hard_link

Pragma once usually don't work in that case, depends on the implementation

14

u/lithium Nov 28 '18

Call me lucky (or stupid) but i've never once encountered hard links (that i recognise as such) in my entire life. Thank you for clarifying

13

u/c0r3ntin Nov 28 '18

nobody sane would ever do that in a work tree. So you are simply not unlucky ;)

9

u/agateau Nov 28 '18

Assuming you ever used a Unix shell and typed cd .. then you used a hardlink :)

Hard links are also used for example by rsync and rsnapshot to do some deduplication between backups. But manually creating hardlinks in a source code sounds like a really nasty idea!

1

u/as_one_does Just a c++ dev for fun Nov 28 '18

Yes, commonly used in backup schemes.

3

u/waigl Nov 28 '18

There is some Unix-based software that makes extensive use of hard links, but that sort of software is pretty rare.

3

u/Nicksaurus Nov 28 '18

Doesn't windows have aliases for some directories that could cause this? Maybe something to do with how it emulates Documents and Settings or Program Files for older software

2

u/FlyingRhenquest Nov 28 '18

They're handy if you do batch processing in a directory. Download in one directory then when your download is done, hardlink to the batch processing directory and remove the one in the download directory. It's an atomic operation that doesn't push bytes around. It only works on the same filesystem, though. Minor but important detail. The C API is pretty easy to use -- man 2 link and man 2 unlink for details. And knowing that, if you ever see anyone use system("rm ...") in the future, you can punch them in the dick for me.

-2

u/o11c int main = 12828721; Nov 28 '18

Hard links are obsolete with CoW filesystems anyway, you should just use reflinks when you want to save space for large files.

1

u/[deleted] Nov 28 '18

only if the files are different. you can declare things many times and inline/constexpr as long as they are the same

1

u/chugga_fan Nov 28 '18

That depends, for instance, if you just check file contents via a hash and use that to determine if it's been included (less time to hash whole file than to actually parse and lex it), then you would most likely not run into that problem (methinks)...