Macros vs inline functions.

Started by
4 comments, last by King Mir 10 years, 2 months ago

Do you think that macros have a place in C++ programming, or are they completely substitutable by inline functions?

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.
Advertisement

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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley


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

... and for being dumb.

#define true false
#define GetMessage GetMessageW


#define BIRD WORD

Sean Middleditch – Game Systems Engineer – Join my team!

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.

This topic is closed to new replies.

Advertisement