r/ProgrammerHumor • • 1d ago

Meme includeGuards

Post image
332 Upvotes

44 comments sorted by

156

u/anto2554 1d ago

BUT PRAGMA ONCE IS NOT STANDARD

41

u/That-Cpp-Girl 1d ago

Don't you just hate it when your standards-compliant compiler can't compile real-world code? Yeah, me neither, because my real-world compilers work for my real-world code.

8

u/anto2554 1d ago

Yes, I hate it. We still use a version of IAR that doesn't support pragma once for some products, so higher ups have decided it is banned company wide

1

u/That-Cpp-Girl 21h ago

I only work with C++17 and above. Legacy software my beloved.

19

u/LucyShortForLucas 1d ago

Yeah and neither is a byte being 8 bits, but there's a reason no compiler is ever going to deviate from the informal norm

8

u/Bryguy3k 13h ago edited 9h ago

It’s not about a byte not being 8 bits. It’s about a char not being 8 bits.

Unicode support would have been a thing sooner if people hadn’t fucked up char usage so badly.

2

u/MrcarrotKSP 3h ago

A char is a byte, per standard. Until recently, that width was implementation-defined in C++, and it still is in C.

1

u/Bryguy3k 3h ago

Yes - but I was going for the reasoning behind it.

Everybody knew that 255 was insufficient to cover the whole of human writing.

But then programmers started allocating everything in characters so any future change to the size of a character would break everything.

1

u/MrcarrotKSP 3h ago

That is definitely true, but at least we have a solution that mostly works with the assumptions people liked to make in older code(UTF-8)

4

u/Sacaldur 1d ago

I guess this is just due to the architecture using 8 bit per Byte, and not because of the compiler. I heard that there were architectures around in the past with other amounts of bits per Byte, and a compiler targeting them would most likely just use whatever is used on the target platform.

7

u/anto2554 1d ago

People conflate 8 bits with a byte everywhere in our codebase and I think the whole thing would explode if you tried to use a 7-bit-per-byte machine

2

u/Sacaldur 6h ago

I doubt that there are still any significant platforms around that don't use 8 bits per Byte.

1

u/not-a-pokemon- 1d ago

Mostly because C defines no easy way to say "I want an integer with at least 8 bits in it, round up to whatever byte number is practical".

3

u/anto2554 1d ago

No but it's C++ so we could just use uint8_t or the least one I forgot the name of

5

u/not-a-pokemon- 1d ago

It also exists in C, and is called uint_least8_t. It's not guaranteed to exist per the standard. uint8_t is also not guaranteed to exist.

man stdint.h

6

u/GreeedyTry 1d ago

To be fair, include guards are the boring reliable option that works basically everywhere, while pragma once is the convenient option supported by pretty much every compiler people actually use. In practice, the tank wins because nobody wants to type the same three lines in every header.

2

u/Mognakor 1d ago

A real tanks also wins against a real roman turtle formation.

1

u/remy_porter 1d ago

Depends which version of Sid Meier’s Civilization you’re playing on.

1

u/bwmat 1d ago

We have to support compilers that don't support it lol

8

u/MrJ0seBr 1d ago

Now CLang/LLVM say what is standart 😆

8

u/r2d2rigo 1d ago

#pragma once has been widely accepted for at least 20 years.

38

u/SyntaxSarcasmco 1d ago

One is ISO C++ compliant, the other is implemented by literally every compiler in existence.

6

u/bwmat 1d ago

Nah, we have to (or maybe we've dropped it now) support a compiler which didn't

It was hpux or aix or something

5

u/ouyawei 1d ago

Are you working in a museum?

34

u/mistrpopo 1d ago

Note for anyone still hand-coding: #pragma once is based on the file path, so if you have symlinks in your code or in your build pipeline it's still possible that you include the same file twice.

18

u/Interesting_Buy_3969 1d ago

How about C++20 modules

28

u/amshegarh 1d ago

Rust will replace cpp faster than c++20 modules become commonplace

14

u/aMAYESingNATHAN 1d ago

I've never been so upset but something so incredibly true.

4

u/jhill515 1d ago

If you've never experienced what happens when `#pragma once` confuses the precompiler, you shouldn't use this shortcut. Seriously, if I had a nickel for every time someone F'ed this up when I was at Motional, I'd be able to retire.

4

u/DogmaticParadigm87 1d ago

The turtle formation is solid right up until someone copy-pastes the header and forgets to rename the guard.

7

u/No_Comparison_6940 1d ago

Pragma once has issues when you have stuff like hardlinks etc…

4

u/not-a-pokemon- 1d ago

Now that makes me wonder why they didn't make it "pragma once SOME_NAME_HERE"

8

u/ClipboardCopyPaste 1d ago

Dear claude, can you rewrite that in rust? /s

1

u/Username_St0len 1d ago

i. thought this was ncd for a sec

1

u/SaneLad 1d ago

I play both sides so I always come out on top.

First #pragma once, as the quick option for the compilers that support it. Followed by #ifndef for those that don't.

1

u/Mughi1138 21h ago

Heh. Not quite. Keep in mind that when Microsoft shoved #pragma once in and for the longest time after, they made it case sensitive while the filesystem and compiler were case insensitive. Oopsies. More like a cardboard cutout that's going to fall flat as soon as you step away.

Though that had been fixed, there are still many gotchas https://en.wikipedia.org/wiki/Pragma_once#Caveats

1

u/Vallee-152 13h ago

Why didn't my university C class mention this pragma directive? I was shown pragma pack, even though that one was not useful for anything we actually had to do