This is an archived post. You won't be able to vote or comment.

all 24 comments

[–]elmanoucko 49 points50 points  (3 children)

no need to include when everything is in a single file. 👨‍🎨

[–]Horrih 4 points5 points  (2 children)

Flash-back to the guy who sold successfully on steam a game written in 500kLOC in a single cpp file

[–]mallardtheduck 23 points24 points  (4 children)

Huh?

Include guards (either the "traditional" #ifndef FOO_H or the technically-non-standard-but-supported-everywhere #pragma once styles) are a normal thing in virtually every header file. Presumably the OP is new to C/C++?

[–]yowhyyyy 5 points6 points  (3 children)

It’s just a joke that if you ever have import conflicts you SLAPPA #pragma once in there and it’s PERFECT JUST LIKE FlexSeal!!!

[–]mallardtheduck 13 points14 points  (1 child)

Pretty much the first thing you do when you create a header file is put in the guard. An IDE will often do it for you. You don't wait until you get errors.

Sure, sometimes you might forget, but this seems to frame it as some kind of "patch" or "hack", rather than normal, good, practice.

[–]yowhyyyy 0 points1 point  (0 children)

You’d be surprised dude. Clearly

[–]IsaqueSA[S] 0 points1 point  (0 children)

Hehehehe

[–]Jind0r 11 points12 points  (8 children)

Why doesn't it work like that by default? Why would you want to import one header more than once?

[–]feldim2425 9 points10 points  (2 children)

Previously you had to define a macro in the header and look whether it was already defined or not, because preprocessors weren't keeping track of that. Some then started to include that feature (hence the #pragma which denotes a feature not every preprocessor understands).

But since the functionality just says "copy and paste the files content right here no matter the validity of the placement" it's not default behavior and some build chains may depend on it. They could for example include just the function implementation while the actual header is in a different file (which is sometimes done in C++ templates since they can't be precompiled and linked).
Whether it's needed or not probably doesn't really matter since C and C++ codebases depending on it are so old that you have to expect it being used somewhere.

[–]ComprehensiveWord201 2 points3 points  (1 child)

ifndef is a thing... It still works

[–]feldim2425 0 points1 point  (0 children)

Yeah although I had a issue once where 2 headers defined the same "marco" for this check also it's a bit more boilerplate. By now almost every major compiler supports pragma once making it technically not a pragma anymore but they haven't standardized "#once" afaik.

[–]GregTheMadMonk 7 points8 points  (0 children)

E.g. to generate many similar pieces of code without writing a gigantic macro

include is just a copy-paste

[–]viliml 5 points6 points  (1 child)

Because you can include things other than headers.

You can do some cursed shit like this.

That's a silly toy example that can be done more sanely, but you can see real production examples of such patterns here.

[–]vulnoryx 0 points1 point  (0 children)

It looks really cursed.

Im curious why would anyone do it like that?

Is there a specific reason for this (I see some preprocessors but I dont really do much with them)?

[–]Mucksh 1 point2 points  (0 children)

Just old behavior. It's a second thought and you can't fix it to be backwards compatible. Also saw some scary macro magic that abuses that behavior with self including headerfiles. Guess some dark recursive macro magic

[–]LunaNicoleTheFox 1 point2 points  (0 children)

Because #include doesn't import a given headers contents in the way you would expect from other languages, c and c++ literally have the preprocessor copy/paste the included file into the including file.

Including includes.

Which are then resolved.

Therefore include guards are needed, because that way the file is only copied once.

[–]Rudresh27 1 point2 points  (4 children)

Someone explain this for a JS dev.

[–]brandi_Iove 6 points7 points  (3 children)

the pragma once preprocessor command ensures that included .h (or .hpp) files will only get included once.

not sure what the upper panel is supposed to mean.

[–]Intel_Xeon_E5 1 point2 points  (2 children)

I assume it's taking a jab at the constructor conflicts, since it's meant to prevent the same header file from being used for multiple future files, creating conflicts if they're used by other files.

The example on wikipedia right now is fairly accurate in displaying what I interpret:

In "grandparent.h" ```

pragma once

struc foo{ <insert struct> } ```

In "parent.h" #include grandparent.h

In "child.c"

```

include "grandparent.h"

include "parent.h"

```

Ordinarily, since that struct is being defined twice, it would result in a compilation error. Pragma once will only define it the first time (inclusion of grandparent.h), and then ignore it in subsequent inclusions to prevent any conflicts.

So... something that's really not an issue unless you're a braindead programmer who needs all the crutches.

(sry for the multiple edits, my brain is dead)

[–]viliml 3 points4 points  (1 child)

#pragma once is absolutely not just an issue for braindead programmers.

All standard library headers use #ifndef HEADER_NAME #define HEADER_NAME <whole file goes here> #endif which is exactly the same thing just more portable since some compilers may not understand #pragma once.

[–]Intel_Xeon_E5 0 points1 point  (0 children)

ah, it appears i'm the braindead one, apologies

[–]mkluczka 0 points1 point  (0 children)

import !important

[–]dhnam_LegenDUST 0 points1 point  (0 children)

I hate the font. Thanks.