0

Possible Duplicate:
Inline functions vs Preprocessor macros

Inline functions were introduced in C++ to replace C style macros (#define macroname...), still, I see lots of C++ code using old C style macros rather than inlined functions, are inlined function inferior to old C style macros?

Community
  • 1
  • 1
snoofkin
  • 8,725
  • 14
  • 49
  • 86

3 Answers3

4

I would say macros are inferior to inlined functions.

Inlined functions are type safe and macros are not. That's the big advantage of them. The compiler is helping you make better code.

With that said, there are a few things that macros can do that functions cannot. There are not many of these things though...

I believe C programmers get used to macros and just continue their habits into C++.

Starkey
  • 9,673
  • 6
  • 31
  • 51
0

The one of the only things I can imagine still using macros for are for the LINE and FILE macros to be incorporated into a logging statement

for example:

#DEFINE TRACE(S) vGenericLogStatement(__FILE__,__LINE__, s)

where the definition of vGenericLogStatement was something like:

void vGenericLogStatement(char* fileName, int line, char* traceMesg);

They are also useful quick and dirty string concatenation.

Dennis
  • 3,683
  • 1
  • 21
  • 43
0

I wouldn't say they are inferior. In C++ there's is not much you can do with a macro that you can't get the same effect with inline functions or templates. The only thing I can think of is the '#' stringize macro operator, and I would t be surprised if Boost has a way to do that, too.

However, when it comes to constants, macros can have one advantage - they usually won't take up any data space, but get written right into the instruction stream. On systems with small memory footprints, this can be a major consideration.

AShelly
  • 34,686
  • 15
  • 91
  • 152