Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eclipse's auto-generated .h file guards

In eclipse, when a c++ class is created, .h file's auto-generated with guard XXXX_H_. In my limited, little experience, the guard is always be in the form of XXXX_H without the trailing _.

So, I'm just curious and wondering why the _ is over there.

Thanks in advance.

like image 491
draw Avatar asked Nov 23 '25 13:11

draw


1 Answers

The trailing _ might be added to avoid collision with user-defined identifiers. For example, you might have a header file named get.h and at the same time you can conceivably have your own macro (or variable, or function) named GET_H. So, using GET_H for include guard in get.h would easily lead to problems.

The standard library header files might use a leading _ to name its internal macros for the very same purpose - to avoid name collision with user-defined identifiers. For that reason, the language specification explicitly prohibits user-defined identifiers that begin with _ and a capital letter. And for the very same reason, the leading _ cannot be used in the names of include guards.

So, Eclipse decided to use a trailing _ for the very same purpose. It provides a reasonable level of protection from name collisions and does not violate the requirements of language specification.

like image 146
AnT Avatar answered Nov 26 '25 03:11

AnT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!