Reputation: 75
could anyone define what is a STRAY DEFINITION? .the second questionis: I'm reading "Teach yourself c++ in 21 days" in the book he said we should not define the term DEBUG. I want to know why?
Upvotes: 0
Views: 519
Reputation: 36487
First part of the question:
A stray definition is a preprocessor definition that changes the behavior of some other code (or the actual code), which most likely won't be intentional. For example, you could write a header file and use the following line:
#define main is_awesome
This won't have any direct impact in your header file, possibly not even in your code, but someone else including this header file in a file containing the function int main(int argc, char **argv)
will run into problems, because this stray definition will change that function's name into int is_awesome(int argc, char **argv)
and suddenly there is no longer a main
entry point for the application!
In a similar way the macro DEBUG
can cause such problems. Typically, DEBUG
should only be defined by the compiler itself, based on whether it's building debug code or not (based on your compiler you might have to set it yourself as well). If you're defining DEBUG
somewhere on your own, you might trigger debug code even though you're actually creating a release build.
In general, such bugs or issues can be really hard to track down, especially if you don't know how to have a look at the preprocessed code (you can't see the problems/errors in your base code and most likely line numbers reported will be off as well).
How to avoid this? Three simple rules that will make your life and that of others a lot easier:
#undef
) preprocessor definition you don't need outside your header file.DEBUG
you could use MYLIB_DEBUG
.Upvotes: 7