Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
The preprocessor directive #define can be used for
* assert
* macro's (not recommended)
The preprocessor statements #ifndef, #define and #endif can be used for #include guards. These prevent a header file to be #included multiple times, causing a redeclaration error. Always write (internal) #include guards [2] [9] .
#ifndef MYHEADERFILE_H
#define MYHEADERFILE_H
//Your header file
#define is used for the debugging #define NDEBUG used by assert (among others). Assert liberally to document internal assumptions and invariants [3] [4] [10] .
//#define NDEBUG //Uncomment to remove all asserts
const double Invert(const double x)
{
assert(x!=0.0); //Divisions by zero are illegal
return 1.0/x;
}
The preprocessor directive #define is used to define macro's.
Avoid macro's [0] [1] [6] as there are better, type-safe alternatives: consts, enums and inlines.
The example code below states that the preprocessor must replace the text 'DOZEN' by the value of twelve.
#define DOZEN 12
Prefer const over this type of #define [0] [6] [7] .
The example code below states that the preprocessor must replace the 'x' between brackets by 'x+1':
#define PLUS_ONE(x) x+1
Prefer inline over this type of #define [0] [6] [8] .
[0] Scott Meyers . Effective C++ (3rd edition). ISBN:0-321-33487-6. Item 2: Prefer consts, enums and inlines to #defines
[1] Bjarne Stroustrup . The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4. Chapter 7.9.4: 'Avoid macro's'
[2] Herb Sutter and Andrei Alexandrescu . C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 24: 'Always write internal #include guards. Never write external #include guards'.
[3] Herb Sutter and Andrei Alexandrescu . C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 68: 'Assert liberally to document internal assumptions and invariants'.
[4] Bjarne Stroustrup . The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4. Advice 24.5.18: 'Explicitly express preconditions, postconditions, and other assertions as assertions'.
[5] Bjarne Stroustrup. The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4. Chapter 7.9.10: 'If you must use macro's, use ugly names with lots of capital letters'.
[6] Jarrod Hollingworth , Bob Swart , Mark Cashman , Paul Gustavson . Sams C++ Builder 6 Developer's Guide. ISBN:0-672-32480-6. Chapter 3, paragraph 'Know when to use the preprocessor', lines 1-2: 'It is not appropriate to use the preprocessor for defining constants or for creating function macro's. Instead, you should use const variables or enum types for constants and use inline function (or inline template function) to replace a function macro.
[7] Jesse Liberty . Sams teach yourself C++ in 24 hours. ISBN:0-672-32224-2. Hour 21, chapter 'Using #define for constants': 'This is almost never a good idea, however, because #define merely makes a string substitution and does no type checking.'
[8] Jesse Liberty . Sams teach yourself C++ in 24 hours. ISBN:0-672-32224-2. Hour 21, chapter 'Macros versus functions and templates': 'Macros suffer from four problems in C++. [...] The final problem, however is the biggest: macros are not type safe. [...] Templates overcome this problem.'
[9] Jesse Liberty . Sams teach yourself C++ in 24 hours. ISBN:0-672-32224-2. Hour 21, chapter 'Inclusion and inclusion guards': 'It never hurts to use inclusion guards. Often they will save you hours of debugging time'. Also: hour 24, chapter 'include guards': 'All header files should use inclusion guards'.
[10] Jesse Liberty . Sams teach yourself C++ in 24 hours. ISBN:0-672-32224-2. Hour 24, chapter 'assert()': 'Use assert freely'.
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.