473,327 Members | 1,920 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,327 software developers and data experts.

inline or not to inline in C++

Hi,
- Are there any cardinal rules as to when a function should not be
inlined?

- If a function, say f, is being called from multiple other
functions/classes, should that function f, be always inlined?

- Does the C++ standard talk about this? What about gcc 3.x
implementation?

- Any resources/pointers would be great help.
Thanks
.....Abhi
Jul 19 '05 #1
2 6224
"Abhi" <a8**@yahoo.com> wrote in message
news:b7**************************@posting.google.c om...
Hi,
- Are there any cardinal rules as to when a function should not be
inlined?
I don't think so. Inlining closely couples the interface and the
implementation. Sometimes that's just what you want:

class Complex // I'm too dumb to use std::complex<double>
{
private:
double m_re, m_im;
public:
Complex(double i_re, i_im) : m_re(i_re), m_im(i_im) {}
double re() const {return m_re;}
double im() const {return m_im;}
};

Here the functions re() and im() give read-only access to the internal
representation of the class. There is no pretense that the real and
imaginary parts are ever going to be implemented as anything but a pair of
doubles. Also, the inlining may improve the performance, and in a concrete
data type like this I don't really need to see profiling data to justify
it -- as far as I am concerned this type of inspector function is just part
of the concrete data type paradigm.

But:

class SomeClass
{
public:
void some_func() {...200 lines of code here...}
};

would almost certainly be a mistake. If the compiler inlines the code (and
after all, you DID ask it to) every call will use a lot of memory and the
function call overhead you saved would be negligible.

- If a function, say f, is being called from multiple other
functions/classes, should that function f, be always inlined?
The more places it is called from the more I would be inclined to avoid
inlining. In principle each call to an inline function creates another copy
of the object code. The thing that suggests inlining is a calling sequence
like:

for (int j = 0; j < 10000; ++j)
sum += obj.f();

If f() is doing something very simple (like just indexing a pointer) the
function call overhead might be a little high. Even then though it is
probably not a big deal.

- Does the C++ standard talk about this? What about gcc 3.x
implementation?
The standard only says what it is. Dunno about gcc.

- Any resources/pointers would be great help.
Thanks
....Abhi

Jul 19 '05 #2
Abhi wrote:
- Are there any cardinal rules
as to when a function should not be inlined?

- If a function, say f, is being called
from multiple other functions/classes,
should that function f, be always inlined?

- Does the C++ standard talk about this?
What about gcc 3.x implementation?

- Any resources/pointers would be great help.
The purpose of inline functions is to discourage
manual inlining of functions by programmers
who are concerned about performance.

Programs are easier the read, understand and maintain
if they are decomposed into re-usable functions.
If the function definitions are visible to the compiler
it can *inline* these functions automatically
to eliminate the extra overhead of a function call
and to permit optimizations that are frustrated
by external function definitions.

The problem with inline functions is that
they must be parsed every time you recompile your program
during program development.
The solution is to define both inline and external version
of your function:
cat file.h #ifndef GUARD_FILE_H
#define GUARD_FILE_H 1

#ifdef EFINE_INLINE
inline
double dot(const double x[], const double y[], int n) {
double t = 0.0;
for (int j = 0; j < n; ++j)
t += x[j]*y[j];
return t;
}
#else //EFINE_INLINE
double dot(const double x[], const double y[], int n);
#endif//EFINE_INLINE
#endif//GUARD_FILE_H
cat file.cc

#undef EFINE_INLINE
#include <file.h>

double dot(const double x[], const double y[], int n) {
double t = 0.0;
for (int j = 0; j < n; ++j)
t += x[j]*y[j];
return t;
}

Application programs quickly compile and link
with the external version of the function
during program development, testing and debugging
then are recompiled with C preprocessor option

-DEFINE_INLINE

just before deployment. This final compilation may be much slower
but the result is code which runs much faster.

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
by: DJ WIce | last post by:
Hi all, I did make a script/css thing to replace the contextmenu on the website with a new one: http://www.djwice.com/contextmenu.html It works nice in MSIE, but on Netscape (and probable...
14
by: Chris Mantoulidis | last post by:
I am not clear with the use of the keyword inline... I believe you add it do a function when you implement the function inside the header file where the class is stored... But is that all? What...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
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...
7
by: Srini | last post by:
Hello, Rules for inline functions say that they have to be defined in the same compilation unit as their declarations. For class member functions this means that the inline member functions must...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
12
by: sam_cit | last post by:
Hi Everyone, I have few questions on inline functions, when i declare a function as inline, is it for sure that the compiler would replace the function call with the actual body of the function?...
5
by: Barry | last post by:
Hi, group First, I write same cases I've already known, I don't concern that specific compiler really do inline or not. Please check them if they are right, and add the cases I miss 1. //...
2
by: Barry | last post by:
Hi, group First, I write same cases I've already known, I don't concern that specific compiler really do inline or not. Please check them if they are right, and add the cases I miss 1. //...
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
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...
0
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.