473,463 Members | 1,508 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 6231
"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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.