We all know when to use include guard, but when shall we not use it in our project?
Recently, I saw a project with mix compilation (CUDA + GCC), one header file (CUDA file) is deliberately left without include guard. I am just curious about it.
We all know when to use include guard, but when shall we not use it in our project?
Recently, I saw a project with mix compilation (CUDA + GCC), one header file (CUDA file) is deliberately left without include guard. I am just curious about it.
There are 2 scenarios off the top of my head:
assert.h
works)In our projects we never use include guard. We are using include antiguard:
#ifndef _stdafx_h_
#define _stdafx_h_
#else
#error reinclude stdafx.h
#endif
Because if you reincluded same header - you written wrong code or worked with wrong architecture.
#include <vector>
in two different files in your projects? –
Headlong <vector>
, even if I'm also including my "algorithms.h" file that also includes <vector>
for some reason. Some style guides, as Google's one, suggest you include all the headers of the data types you use. –
Headlong sed
this in my own headers in my current project to see whether I'm doing anything redundant or near-circular. In theory, as you said, for non-library/-template headers, reinclusions should not occur. –
Leix stdafx.h
sounds very microsoft visual studio, what if you are not writing code in MSVS and precompiled headers are irrelevant? - is your "use case" specific to MSVS? I don't really see any issue refactoring. I refactor stuff all the time - change a few header names here and there (compiler will tell you if there is one wrong)...done. What is much more annoying is the header guards, but that is fixed with #pragma once
. Also if you want to extract a bit of code, you can see immediately what sort of headers you will need without having to analyse the whole project. –
Herschel One case in when you do want to include the same file several times with different parameters. In this case the include file would act as a sort of template. An example are the scalers on Dosbox.
Include guards are used so that the include file can be included multiple times in a single compilation unit without resulting in duplicate declarations.
Do not use include guards when the file should be included multiple times in a single compilation unit and this does not result in duplicate declarations.
© 2022 - 2024 — McMap. All rights reserved.
x_
orxx_
in some conventions); see also here. Boost.Preprocessor shows how to take this to the extreme. – Homunculus