Do you think that macros have a place in C++ programming, or are they completely substitutable by inline functions?
Macros vs inline functions.
Don't use macros for inline functions.
#define is still useful in C++ for:
1) Compile time switches via #if/#ifdef etc.
2) Token pasting and stringization operators ##, #. In C you can use those for template-style stuff but you would use actual templates for that in C++. They are sometimes used in C++ for generating class names for unit test frameworks etc.
2) Token pasting and stringization operators ##, #. In C you can use those for template-style stuff but you would use actual templates for that in C++. They are sometimes used in C++ for generating class names for unit test frameworks etc.
...and for Assert messages.
... and for being dumb.
#define true false #define GetMessage GetMessageW
#define BIRD WORD
Macros are the last resort tool to reduce code duplication. Functions are always preferable when possible, but sometimes they aren't.
For example, if you want a bunch of template specializations for primitive types that are structurally identical, you can use a macro.
For example, if you want a bunch of template specializations for primitive types that are structurally identical, you can use a macro.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement