473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

when to use inline and when to #define

arunmib
104 100+
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
5 1595
Meetee
931 Expert Mod 512MB
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
104 100+
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
9,208 Expert Mod 8TB
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
463 Expert 256MB
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
104 100+
ok.....thanks for the info.... :)
Aug 16 '07 #6

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

Similar topics

2
by: sks_cpp | last post by:
I want to inline a function operator (part of a functor class) that does a push_back on a list container. Would the compiler inline this method or not? How can I tell? Since the method...
354
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets...
8
by: Sreenivas | last post by:
Hi, We cannot return a reference to an automatic variable from a function, as per the ANSI C++ standard the behaviour is undefined. Does this hold for inline functions too? or can I return a...
3
by: Peng Yu | last post by:
Hi, I'm trying to define inline_test::test() to be inline. But I always got errors. I know that if I define inline_test::test() in inline_test.h, there are no errors. But I still would rather...
20
by: Grumble | last post by:
Hello everyone, As far as I understand, the 'inline' keyword is a hint for the compiler to consider the function in question as a candidate for inlining, yes? What happens when a function with...
4
by: Nish | last post by:
Hi, I have a question regarding inline functions in C. If I declare a function as inline in the header file but do not define it there. Then I define that function in .c file, however neither...
4
by: Neo | last post by:
Hi All, Can I write a variable argument function as inline function? and will it be inline always? if not, can I force it to be line on all platforms.
9
by: Martin Wells | last post by:
Is there anything like __inline_is_supported to indicate whether a certain C compiler supports inline? I'm writing my code as fully portable C89, but I want to use inline. Martin
2
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.