As multiple comments have already said, the purpose of this structure is as an "include guard". This ensures that a header is not included twice in same translation unit.
Say you have a.h
which includes t.h
file, also b.h
which includes t.h
file.
When a foo.cpp
include both a.h
and b.h
two copies of t.h
file is included, but due to this include guard, t.h
will only be compiled once. This will save in compile time and prevent multiple definition or declaration errors.
Regarding #define
needing a value, it is not so. in this case if you use T2_A
somewhere in your code, it will be replaced with nothing, not even a space.
These can be usefull in diagnostics like so:
#ifdef _DEBUG
#define LOG(x) std::cout << (#x) << " " << (x)
#else
#define LOG(x)
#endif
Here you may insert LOG(x)
anywhere in your code to display the value passed to the macro. When _DEBUG
is define (ie building for diagnostics) the program will log values. But when building for final release, _DEBUG
is not defined and all logging statements are replaced with nothing, like it was never there.