buszkiraly
buszkiraly

Reputation: 193

Is it possible to never use header guards in C/C++? If so, does this approach have any advantage?

I have recently gotten into a project where the usage of header guards is forbidden as it is "bad design". Custom types are forward declared and headers are only included in cpp files.

This is the first project I have seen doing this and I find it quite uncomfortable to work with and if it were up to me I'd introduce the guards.

Using them has always been a given for me, so I don't even know how to argue against not using them other than highlighting the fact that it's uncomfortable. The project clearly compiles so in this case it's doable, but I'm left with a couple of questions:

Upvotes: 3

Views: 193

Answers (2)

Alex Guteniev
Alex Guteniev

Reputation: 13634

I guess not including header in other header would cause maintaining list of headers in each translation unit (.cpp file).

If some dependency in one class on other is introduced, each header list is to be updated. Similarly, to take advantage of broken dependency, each list has to be updated too.

This does not look like a "good design".

Upvotes: 2

ypnos
ypnos

Reputation: 52337

Header guards are not "bad design". Refering to Bjarne Stroustrup and Herb Sutter:

SF.8: Use #include guards for all .h files

I would love to see the people behind your new project to argue with Stroustrup and Sutter over what is "bad design" in C++.

Forward declarations are a great tool, yes. But certainly not an answer to all situations. How would that work? You only have pointers as class members? You meticulously ensure nothing is defined in a header file?

Upvotes: 1

Related Questions