Is an inline function really the same as a macro?

This is a discussion on Is an inline function really the same as a macro? within the C Programming forums, part of the General Programming Boards category; I'm wondering if an inline function is really the same as a preprocessor macro - just with some advantages like ...

An inline function is a feature of C++, not C. The compiler can choose not to inline a function you've declared as inline if it wants. However, the compiler won't turn a macro into a genuine function because it's just preprocessor stuff.

Re: Is an inline function really the same as a macro?

Originally posted by Sloede I'm wondering if an inline function is really the same as a preprocessor macro - just with some advantages like type checking et cetera.

No. They are very very different. When working with inlined functions, think of it just like it's a regular function in terms of how the data is manipulated, with the desired effect of having the code inserted at the desired location rather than issuing a function call to do the job (although simply using inline doesn't guarantee this result).

A macro, on the otherhand, is expanded before the program is even compiled and the parameters work as though they are textually inserted into the newly created code. Your example, for instance, does this. Also, macros don't obey scope rules.

Other differences are that you can take the address of inline functions, whereas you can't take the address of macros. Also, with macros, you can use parameters that you can't easily do with inline functions such as datatype names, expressions, etc. Also, because a macro can be looked at as a textual insert, you can use the results of macros in places where you can't use inline functions. Macro results can be compile-time constants whereas inline function return values can not.

In C++ this "problem" of not being able to use the result where a constant is needed without using a macro can be solved by using template classes with nested constants. This solution takes the benefits of type-safety that inline functions provide, but still allows the result to be used in a constant expression. Macros, be gone!

Some other properties of the template example are that no matter how many times you use string_size< 255 >, the calculation num_characters + 1 will only occur one time, unlike with the macro solution. In this situation the calculation is simple so it won't make much of a difference, but in other cases it can be a fairly complex calculation. Also, since the 255 is a template parameter, it must be a compile-time constant, so you can not use it if the value 255 is stored in a non-const variable (or a const variable initialized via a non-const variable). Lastly, if your calculation requires floating point constants instead of integral types, you can't use the template solution because floating point types can not be used as template values.

In the end, if you are using C, then there are many cases where you may have to use macros simply because there is no more elegant solution available. In C++, there are still some times where you must use macros (IE to get functionality like assert, etc), but with templating, those situations are considerably less.

Sorry to go off on a tangent if it was more information than you wanted.

You're kidding me! That answer was bloody awesome! Thank you very much!

I have to implement an algorithm in C and it uses some really basic functions in each step (like a one-line expression). But the use of macros somehow displeases me as it is like the "dirty" solution. So I thought about using inline-functions (as part of the standard of course) when my initial question arose.