3 ms·
> #pragma once Is there every a scenario where this is not needed in a header file?
by sudeepj 6y ago
> #pragma once
Is there every a scenario where this is not needed in a header file?
- cozzyd 6y agosome headers are meant to be included multiple times with different definitions (although templates are usually a better way to do this).
- dkersten 6y agoYeah I used to see it from time to time, but it’s been w long time now. It seems to me that modern C++ doesn’t really need it unless you’re doing something like including data files (rather than source files) multiple times. Having said that, I almost did something like that recently, to initialise some generated data structures (include once for the structs and again to initialise memory pools), but I ended up finding a cleaner way that didn’t need it.
- OskarS 6y agoIt happens occasionally. An example would be that there are a couple of libraries that do "generic collections" in C using the preprocessor (instead of using templates). So, you could imagine something like this: #define VEC_TYPE double #include <c-vector.h> #undef VEC_TYPE and that would provide you a type like "vec_double", a vector specialized for the double type. You'd want to import that once for each vector type you wanted. It's not the most common use-case (and you can argue if it's a good idea at all), but it is at least a reasonable reason why you'd want a header file without #pragma once.
- glouwbug 6y agoYe, I pushed it to the extreme and back ported a good chunk of the STL to C: https://www.github.com/glouw/ctl https://www.github.com/glouw/ctl