An inline function is defined by the inline keyword. Whereas the macros are defined by the #define keyword. 2. Through inline function, the class's data members can be accessed.
1) No. 2) A Macro in C is simply text that is expanded before the compiler processes the source code. The inline keyword is used as a hint to the compiler that the function can be placed inline without the need for a call-stack to be set up.
By declaring a function inline, you can direct GCC to integrate that function's code into the code for its callers.
Macro: a word defined by the #define preprocessor directive that evaluates to some other expression. Preprocessor directive: a special #-keyword, recognized by the preprocessor. Show activity on this post. preprocessor modifies the source file before handing it over the compiler.
Preprocessor macros are just substitution patterns applied to your code. They can be used almost anywhere in your code because they are replaced with their expansions before any compilation starts.
Inline functions are actual functions whose body is directly injected into their call site. They can only be used where a function call is appropriate.
Now, as far as using macros vs. inline functions in a function-like context, be advised that:
First, the preprocessor macros are just "copy paste" in the code before the compilation. So there is no type checking, and some side effects can appear
For example, if you want to compare 2 values:
#define max(a,b) ((a<b)?b:a)
The side effects appear if you use max(a++,b++)
for example (a
or b
will be incremented twice).
Instead, use (for example)
inline int max( int a, int b) { return ((a<b)?b:a); }
The Inline function are expanded by the compiler where as the macros are expanded by the Preprocessor, which is mere textual substitution.Hence
There is no type checking during macro invocation while type checking is done during function call.
Undesired results and inefficiency may occur during macro expansion due to reevaluation of arguments and order of operations. For example
#define MAX(a,b) ((a)>(b) ? (a) : (b))
int i = 5, j = MAX(i++, 0);
would result in
int i = 5, j = ((i++)>(0) ? (i++) : (0));
The macro arguments are not evaluated before macro expansion
#define MUL(a, b) a*b
int main()
{
// The macro is expended as 2 + 3 * 3 + 5, not as 5*8
printf("%d", MUL(2+3, 3+5));
return 0;
}
// Output: 16`
The return keyword cannot be used in macros to return values as in the case of functions.
Inline functions can be overloaded
The tokens passed to macros can be concatenated using operator ## called Token-Pasting operator .
Macros are generally used for code reuse where as inline functions are used to eliminate the time overhead (excess time) during function call(avoiding a jump to a subroutine).
The key difference is type checking. The compiler will check whether what you pass as input values is of types that can be passed into the function. That's not true with preprocessor macros - they are expanded prior to any type checking and that can cause severe and hard to detect bugs.
Here are several other less obvious points outlined.
To add another difference to those already given: you can't step through a #define
in the debugger, but you can step through an inline function.
Macros are ignoring namespaces. And that makes them evil.
inline functions are similar to macros (because the function code is expanded at the point of the call at compile time), inline functions are parsed by the compiler, whereas macros are expanded by the preprocessor. As a result, there are several important differences:
In some cases, expressions passed as arguments to macros can be evaluated more than once. http://msdn.microsoft.com/en-us/library/bf6bf4cf.aspx
macros are expanded at pre-compile time, you cannot use them for debugging, but you can use inline functions.
-- good article: http://www.codeguru.com/forum/showpost.php?p=1093923&postcount=1
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With