Is an inline function really the same as a macro?
I'm wondering if an inline function is really the same as a preprocessor macro - just with some advantages like type checking et cetera.
Would that mean if I define
Code:
inline void double_it(int a) {
a += a;
}
and I use it like this
Code:
int a = 5;
double_it(a);
that my var 'a' would be actually 10 afterwards? Or would I still need to use pointers?
This question just came up when I read through some C tutorial - and I'm almost sure that this isn't working :)
Re: Is an inline function really the same as a macro?
Quote:
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.
Example:
Code:
#include <stddef.h>
#define STRING_SIZE( num_characters ) (num_characters) + 1
inline const size_t string_size( size_t num_characters )
{
return num_characters + 1;
}
char String[STRING_SIZE(255)]; // compiles
//char String[string_size(255)]; // won't compile
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!
C++ example:
Code:
#include <cstddef>
template< std::size_t num_characters >
class string_size
{
public:
static const std::size_t result = num_characters + 1;
};
char String[string_size< 255 >::result]; // works
Of course, that's a C++ example so in C that's not an option.
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.