By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,883 Members | 2,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,883 IT Pros & Developers. It's quick & easy.

when to use inline and when to #define

arunmib
100+
P: 104
Hi all,
I have a doubt? When do I use an inline function and when do I use a #define ? I have a fair idea about their differences, but I would like to know when to use what?

One point i know is, when you need to do debugging use 'inline' and not a macro....Other than that ?
Aug 14 '07 #1
Share this Question
Share on Google+
5 Replies


Meetee
Expert Mod 100+
P: 931
Hi all,
I have a doubt? When do I use an inline function and when do I use a #define ? I have a fair idea about their differences, but I would like to know when to use what?

One point i know is, when you need to do debugging use 'inline' and not a macro....Other than that ?
Hi,

Expressions passed as arguments to inline functions are evaluated once. In some cases, expressions passed as arguments to macros can be evaluated more than once.

Inline follows strict parameter type checking, macros do not.

Inline functions are more safe then macros. Many programers strictly believe in not to use macros!

Regards
Aug 14 '07 #2

arunmib
100+
P: 104
Hi,

Expressions passed as arguments to inline functions are evaluated once. In some cases, expressions passed as arguments to macros can be evaluated more than once.

Inline follows strict parameter type checking, macros do not.

Inline functions are more safe then macros. Many programers strictly believe in not to use macros!

Regards
hey thanks for the reply......but this is not what i have been asking for. What I meant is there must be situation in which the usage of "macros" is preferred and in others "inline" usage will be the best.....
say in Situation X use "macro" and not "inline", cause usage of "inline" might degrade the efficiency of the code (for eg.....)
and in Situation Y use "inline" and not "macro" say for security reasons...etc....
I think I have made myself clear ppl....some information please....
Aug 14 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
In C++ you do not use macros.

Macros have been replaced by these C++ features:
1) inline functions
2) templates
3) enums
4) namespaces.

The problem with macros is that the macro is expanded by the preprocessor wo what is compiled is not what is in your source code. If the macro expansion causes problems, your debugger is useless since there is no code to debug. Should to get problem like this, you have to stop be build after the proeprocessor is finished and before the compiler executes annd verify by eyeball (a very relaible method) each and every macro expansion to see if it was done correctly.

The only place in C++ where a macro is used is in the inclusion guards of header files:
Expand|Select|Wrap|Line Numbers
  1. #ifndef MYHEADER
  2. #define MYHEADER
  3. ...header file contents...
  4. #endif
  5.  
C does not have these features so C programmers use macros all the time. When these programmers learn C++, they bring their bad habits into the C++ code and riddle it with macros which propagates the problems they had in C.
Aug 14 '07 #4

RRick
Expert 100+
P: 463
Inlines are used a lot with GNU compilers when using templates. When GNU compiles template code all the source code must be in the header file. You can not break up GNU template code between .h and .cpp files. Inlines also have the promise of being faster than calling a method, but this performance increase is minor.

Macros/Defines have gotten a bad name (and rightfully so) over the years. They are the orgin of weird and obscure bugs and are difficult/painful to debug. Too many times, they are used as a hack to fix a bug or system problem. They can also take reasonable code and make it unreadable. Use them as a last resort, only.
Aug 15 '07 #5

arunmib
100+
P: 104
ok.....thanks for the info.... :)
Aug 16 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.