473,770 Members | 3,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

To inline or not to inline?

Hello all,

I'm curious as to your opinions on explicitly inlining function? I'm talking
about functions as members of a class. For example, so class A defines a
operator==() and a operator!=():

class_a.h:

class A
{
public:
A();
bool operator==(A &rhs);
inline bool operator!=(A &rhs);
...
};

bool A::operator!=(A &rhs)
{
return !(*this == rhs);
}

Is it something that should be done by the programmer or should we assume
that compiler optimisations will take care of it?

--
Alvin
Jul 23 '05 #1
7 2035
* Alvin:

I'm curious as to your opinions on explicitly inlining function? I'm talking
about functions as members of a class. For example, so class A defines a
operator==() and a operator!=():

class_a.h:

class A
{
public:
A();
bool operator==(A &rhs);
inline bool operator!=(A &rhs);
...
};

bool A::operator!=(A &rhs)
{
return !(*this == rhs);
}

Is it something that should be done by the programmer or should we assume
that compiler optimisations will take care of it?


The keyword 'inline' is not primarily about optimization.

See section 3 of chapter 2.1 (i.e. section 2.1.3) of my attempted correct
C++ tutorial at

<url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_02.ht ml>

and also the FAQ at

<url: http://www.parashift.c om/c++-faq-lite/inline-functions.html>

(The tutorial refers to the FAQ, and the FAQ refers to the tutorial, so if
you like to go in circles you can follow the links... ;-) )

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
Alvin wrote:
Hello all,

I'm curious as to your opinions on explicitly inlining function? I'm talking
about functions as members of a class. For example, so class A defines a
operator==() and a operator!=():

class_a.h:

class A
{
public:
A();
bool operator==(A &rhs);
inline bool operator!=(A &rhs);
...
};

bool A::operator!=(A &rhs)
{
return !(*this == rhs);
}

Is it something that should be done by the programmer or should we assume
that compiler optimisations will take care of it?


In my opinion, functions and methods should be inlined
when the execution cost of their content is less than
or equal to the function call & return overhead; provided
the function has already been tested as a non-inlined
function.

Exceptions:
1. Development.
Many compilers have problems providing debugging
information for inlined functions.

2. Deliverable headers.
Interfaces for the external customer, such as
libraries, should not have inlined functions;
or change the design so that the delivered headers
do not contain inlined code.

Again, this is my opinion. Yours may differ.

Remember that the keyword "inline" is only a suggestion
to the compiler. The compiler may already be inlining
the function (or eliminating it).

Don't worry about optimizations until the project
works correctly.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Jul 23 '05 #3
Alvin wrote:
I'm curious as to your opinions on explicitly inlining function?
They only way to explicitly inline code is to manually inline code.
I'm talking about functions as members of a class.
For example, so class A defines
operator==() and operator!=():
> cat class_a.h:
class A {
public:
A(void);
bool operator==(cons t A& rhs) const;
inline // not necessary
bool operator!=(cons t A& rhs) const;
// ...
};

inline bool A::operator!=(c onst A& rhs) const {
return !(*this == rhs);
}

Is it something that should be done by the programmer
or should we assume that compiler optimisations will take care of it?
Your compiler may choose to inline operator!= automatically
even if you don't use the inline qualifier.
Your compiler may choose *not* to inline operator!= automatically
even if you *do* use the inline qualifier.

The real purpose of the inline qualifier
is to help the compiler with *linkage*.
If you include your header file in two different translation units
then try to link them together,
you'll get error messages from your link editor:
cat class_a.h #ifndef GUARD_CLASS_A_H
#define GUARD_CLASS_A_H 1

class A {
private:
// representation
int I;
public:
// operators
bool operator==(cons t A& rhs) const;
bool operator!=(cons t A& rhs) const;
// constructors
A(int i): I(i) { }
};

bool A::operator!=(c onst A& rhs) const {
return !(*this == rhs);
}

#endif//GUARD_CLASS_A_H
cat class_a.cpp #include "class_a.h"

bool A::operator==(c onst A& rhs) const {
return I == rhs.I;
}
g++ -Wall -ansi -pedantic -c class_a.cpp
cat main.cpp #include <iostream>
#include "class_a.h"

int main(int argc, char* argv[]) {
A x(13), y(42);
std::cout << (x != y) << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cpp class_a.o

class_a.o(.text +0x0): \
In function `A::operator!=( A const&) const':
: multiple definition of `A::operator!=( A const&) const'
/tmp/cc6QaYwA.o(.tex t+0x100): first defined here
collect2: ld returned 1 exit status

Use the inline qualifier for A::operator!=
and the problem goes away.
Jul 23 '05 #4
Thomas Matthews wrote:
In my opinion, functions and methods should be inlined
when the execution cost of their content is less than
or equal to the function call & return overhead; provided
the function has already been tested as a non-inlined
function.

Exceptions:
1. Development.
Many compilers have problems providing debugging
information for inlined functions.

2. Deliverable headers.
Interfaces for the external customer, such as
libraries, should not have inlined functions;
or change the design so that the delivered headers
do not contain inlined code.

Again, this is my opinion. Yours may differ.

Remember that the keyword "inline" is only a suggestion
to the compiler. The compiler may already be inlining
the function (or eliminating it).

Don't worry about optimizations
until the project works correctly.


You are confused.
Take a look at any quality implementation of the standard library.
You will find that it depends upon heavy use of inline functions
to make the code easier to read, understand and maintain
without sacrificing performance or efficiency.

The real benefit of inline functions [and operators]
is that they allow programmers to decompose large functions
into smaller functions without concern for how
doing so will effect performance.

I prefer to make all functions inline functions
and let the optimizing compiler decide
whether to inline them or not.
Jul 23 '05 #5
In message <n0************ ***@newssvr17.n ews.prodigy.com >, Thomas
Matthews <Th************ *************@s bcglobal.net> writes
Alvin wrote:
Hello all,
I'm curious as to your opinions on explicitly inlining function? I'm
talking
about functions as members of a class. For example, so class A defines a
operator==() and a operator!=():
class_a.h:
class A
{
public:
A();
bool operator==(A &rhs);
inline bool operator!=(A &rhs);
...
};
bool A::operator!=(A &rhs)
{
return !(*this == rhs);
}
Is it something that should be done by the programmer or should we
assume
that compiler optimisations will take care of it?

In my opinion, functions and methods should be inlined
when the execution cost of their content is less than
or equal to the function call & return overhead; provided
the function has already been tested as a non-inlined
function.

Exceptions:
1. Development.
Many compilers have problems providing debugging
information for inlined functions.

2. Deliverable headers.
Interfaces for the external customer, such as
libraries, should not have inlined functions;
or change the design so that the delivered headers
do not contain inlined code.


3. Compilation time considerations.
Defining functions inline may require your headers to #include many more
files, which can adversely affect the compilation time of everything
which includes them. If the (non-inline) function is defined elsewhere,
separating implementation from interface, this doesn't arise.
Again, this is my opinion. Yours may differ.

Remember that the keyword "inline" is only a suggestion
to the compiler. The compiler may already be inlining
the function (or eliminating it).

Don't worry about optimizations until the project
works correctly.


--
Richard Herring
Jul 23 '05 #6
In message <d5**********@n ntp1.jpl.nasa.g ov>, E. Robert Tisdale
<E.************ **@jpl.nasa.gov > writes
Thomas Matthews wrote:
In my opinion, functions and methods should be inlined
when the execution cost of their content is less than
or equal to the function call & return overhead; provided
the function has already been tested as a non-inlined
function.
Exceptions:
1. Development.
Many compilers have problems providing debugging
information for inlined functions.
2. Deliverable headers.
Interfaces for the external customer, such as
libraries, should not have inlined functions;
or change the design so that the delivered headers
do not contain inlined code.
Again, this is my opinion. Yours may differ.
Remember that the keyword "inline" is only a suggestion
to the compiler. The compiler may already be inlining
the function (or eliminating it).
Don't worry about optimizations
until the project works correctly.


You are confused.
Take a look at any quality implementation of the standard library.
You will find that it depends upon heavy use of inline functions
to make the code easier to read, understand and maintain
without sacrificing performance or efficiency.

The real benefit of inline functions [and operators]
is that they allow programmers to decompose large functions
into smaller functions without concern for how
doing so will effect performance.

I prefer to make all functions inline functions
and let the optimizing compiler decide
whether to inline them or not.


Some of us prefer to separate interface and implementation, and not
worry about optimization until we know it's needed. Inline functions
increase coupling, which is usually regarded as a bad thing.

--
Richard Herring
Jul 23 '05 #7
Richard Herring wrote:
3. Compilation time considerations.
Defining functions inline may require your headers to #include many more
files, which can adversely affect the compilation time of everything
which includes them. If the (non-inline) function is defined elsewhere,
separating implementation from interface, this doesn't arise.
You need to ask yourself a question,

"Do I really care about performance?"

If you do, you will probably want to use
as many inline function definitions as possible.
You might consider taking advantage of both
inline *and* external function definitions:
cat file.h #ifndef GUARD_FILE_H
#define GUARD_FILE_H 1

#ifdef EFINE_INLINE
inline
double f(double x) {
return x*(x + 2.0) + 1.0;
}
#else //EFINE_INLINE
double f(double x);
#endif//EFINE_INLINE
#endif//GUARD_FILE_H
cat file.cc #undef EFINE_INLINE
#include "file.h"

double f(double x) {
return x*(x + 2.0) + 1.0;
}
g++ -DEFINE_INLINE -Wall -ansi -pedantic -O3 -c file.cc
nm --demangle file.o

00000000 T f(double)

This allows your inline and external function definitions
to coexist peacefully.
Use the -DEFINE_INLINE option only after you have finished
testing and debugging all of your code.
This will speed up the program development cycle
and allow you to optimize your code just before deployment.
Jul 23 '05 #8

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

Similar topics

46
7043
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 other browsers) it stays on the same place (does not "open'' where the mouse is). And the links do not work when you click on them. If anyone has sugestions on how to improve it, please let me know.
14
2788
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 am I missing? If that's all, then why did Bjarne even bother adding it to the language? If that's not all, what else can I do with "inline"?
47
3885
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
20
3154
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 extern linkage is inlined? Should the compiler still export the function? Or is an inlined function implicitly static?
7
2862
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 be defined either within the class or within the same header file. But its generally a good programming practice to have the declarations and definitions in seperate files. This would make the future maintenance of the code easier.
6
4008
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 as well. To support thin-template, I need to make virtual function as inline. Now, I know that compiler would generate an out-of-line copy when it
12
676
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? or is it a call taken by compiler? Second, i see that it is same as what Macro's used to do for c, if so what is the advantage for going in for inline functions than to Macros?
5
1923
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. // a.h inline void f() {}
2
1557
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. // a.h inline void f() {}
9
2129
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
9591
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10228
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3970
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 we have to send another system
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.