Reputation: 741
I am reading the book C++ Coding Standards: 101 Rules, Guidelines, and Best Practices, and it says that using #define
is bad to use. When I was looking at some of the header files they have many #define
s. If it's bad to use #define
s, why is there so many? Thank you.
Upvotes: 4
Views: 474
Reputation: 206526
#define
are a bad practice because:
They don't have any Scope:
#define
s don't respect scopes so there is no way to create a class scoped namespace. While variables can be scoped in classes.
Weird magical numbers during compilation errors:
If you are using #define
those are replaced by the pre-processor at time of precompilation So if you receive an error during compilation, it will be confusing because the error message wont refer the macro name but the value and it will appear a sudden value, and one would waste lot of time tracking it down in code.
Debugging Problems:
Also for same reasons mentioned in #2, while debugging #define
won't provide much of an help really.
Hence it is much better idea to use const
variables instead of a #define
.
They are superior to #define
in all above mentioned aspects.Only areas where #define
can be really helpful are where you need actual textual replacement in code or in defining include header guards.
Why are
#define
widely used in C standard header files?
One reason that comes to my mind is, In C(unlike C++) const
declarations do not produce constant expressions.Which means prior to introduction of Variable length arrays in C standard one cannot write something like:
const int max_val = 100;
int foos[max_val];
because in C max_val
is not a compile time constant, and prior to introduction of VLA's array subscripts were needed to be compile time constants.
So one had to write this instead as:
#define MAX_VAL 100
int foos[MAX_VAL];
Upvotes: 6
Reputation: 211580
What that's probably referring to is the old C way of defining constants:
#define MAX_SOMETHING 100
int x = MAX_SOMETHING;
These constants aren't typed, they're expanded in place using a string substitution, and make it harder to debug since once the source is compiled it's not clear where that definition originated.
A more C++ way of doing it is:
const int max_something = 100;
int x = max_something;
Since this is a strongly typed value it is subject to all the required checks and appropriate conversions if required.
An additional benefit is that const
values can be put into namespaces and classes for organizational purposes. A #define
is global in scope so collisions are a concern, something that leads to awkwardly long names to avoid conflict.
Between const
and template
, which allows for a form of meta-programming C doesn't do natively, the number of occasions where #define
is required is quite diminished. It's not entirely eliminated though, as without having the #import
directive you will still need to add the old #ifndef __HEADER_FILE_NAME__
guards to ensure things aren't included twice.
Upvotes: 2
Reputation: 23868
The broad statment of the book is not so true - #define
has its place for macro etc but for defining constants it is now not a good idea to use
eg
#define FOO 257
is better done at
const int FOO=257;
This allows type checking because with the #define this becomes a bit odd
char c=FOO;
Upvotes: 0