3 ms·
What's wrong with `#pragma once`? (honest question, i only do C and C++ as a hobby)
by tobyhinloopen 2y ago
What's wrong with `#pragma once`? (honest question, i only do C and C++ as a hobby)
- Gibbon1 2y agoNothing is wrong with that.
- eqvinox 2y agoOne thing is wrong with it: a large part of C developers either doesn't know it exists, or doesn't know what it does. … which I'd fix by using it more :D
- kevin_thibedeau 2y agoIt doesn't exist in C. It's solely a compiler extension. Include guards get the same performance benefit as #pragma once on compilers that support it. There's no point in embracing laziness with no upside.
- eqvinox 2y agoThere is an upside: complex header files often contain a bunch of #ifdef … #endif blocks, especially when dealing with cross-platform support. Since indentation is generally not used there, they can be rather hard to read, especially when nested. Not having the include guard use the same mechanism makes these a bit easier to read and removes a little bit of cognitive load. (And every bit of cognitive load counts since that's a very hard limit of people's brains.) Personally (and for the projects I work on), I couldn't care less whether it exists in ISO C or not. I have a list of compilers and platforms I want/need to support. But of course your project may be different.
- kevin_thibedeau 2y ago> Since indentation is generally not used there, Then indent your macros for readability. clang-format can do it for you if you don't want to maintain them.
- eqvinox 2y agoThanks but I'm not asking for a better way to handle #ifdef's, indenting them is extra work¹ and there's still an unnecessary outermost block for the include guard. You've also not made any further argument against the use of "#pragma once". ¹ especially across an open source project with more than a hundred contributors from a multitude of companies.
- emsy 2y agoYou can't target ancient compilers.
- uecker 2y agoIt is not supported everywhere and it uses some heuristic to determine that the file is actually the same one as used before, and this heuristic sometimes fails in complicated scenarios, e.g. with multiple shared directories, link, or because a file was copied.
- iainmerrick 2y agoPedants don’t like it.