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

Why no inline function body in a C++ file?

Usually it is common to write the class member function in the class H file,
but some people like to write the function body in the C++ file.

Can anybody tell me what are the cases where inline function should not be
written in the C++ file? Or what are the disadvantages of inline function
body in a C++ file?

--
Regards
Sharon G.
Nov 17 '05 #1
6 2076
Sharon wrote:
Usually it is common to write the class member function in the class
H file, but some people like to write the function body in the C++
file.

Can anybody tell me what are the cases where inline function should
not be written in the C++ file? Or what are the disadvantages of
inline function body in a C++ file?


I'll assume that by "C++ file" you mean ".CPP" file. The .H file is just as
much C++.

Inline function bodies have to be visible to the compiler in every
translation unit where they're used. The most common way to ensure that is
to simply put the inline definitions in the same header file where they're
declared.

If a function has been declared as inline but the definition is not visible
to the compiler, the compiler simply cannot continue: since the function is
inline, the compiler can't generate a call to an out of line funciton,
neither can it make something up (of course).

If a function is used in only one .CPP file, that function's definition can
of course be placed in that .CPP file. The disadvantage to doing that, of
course, is that if you subsequently want to use that function in another
..CPP file, you'll have to move the definition. The wrong solution is to put
duplicate definitions of the function into two files. That's just asking
for trouble down the line when someone modifies one copy of the function and
not another. It's also a violation of what's known as the "One Definition
Rule".

-cd
Nov 17 '05 #2
Hi Carl,

I think it is possible to write the inline function implementation in the
CPP file, and it will usually be OK.

But in some cases, like templates, the inline will not work.

Personally I’m using the inline only at the H file, but some college of mine
need to convince, and I do not remember the convincing reasons.

So I need some specific example for cases where the inline implementation in
the CPP is causing trouble. And the more examples the more convincing I will
be.

------
Thanks
Sharon
Nov 17 '05 #3
Sharon wrote:
Hi Carl,

I think it is possible to write the inline function implementation in
the CPP file, and it will usually be OK.
Only if the function is only called from within that file.
But in some cases, like templates, the inline will not work.
It will if the template is only used in that file - template are really no
different from ordinary inline functions in this case.
Personally I'm using the inline only at the H file, but some college
of mine need to convince, and I do not remember the convincing
reasons.

So I need some specific example for cases where the inline
implementation in the CPP is causing trouble. And the more examples
the more convincing I will be.


The cases where it doesn't work should be pretty convincing.

// A.h

inline void f();

// A.cpp

#include "A.h"

inline void f()
{
}

// B.cpp

#include "A.h"

void foo()
{
f(); // linker error
}

-cd
Nov 17 '05 #4
What about link time optimization, Carl? I thought this way a function from
another object file can be inlined.

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:e1**************@tk2msftngp13.phx.gbl...
Sharon wrote:
Hi Carl,

I think it is possible to write the inline function implementation in
the CPP file, and it will usually be OK.


Only if the function is only called from within that file.
But in some cases, like templates, the inline will not work.


It will if the template is only used in that file - template are really no
different from ordinary inline functions in this case.
Personally I'm using the inline only at the H file, but some college
of mine need to convince, and I do not remember the convincing
reasons.

So I need some specific example for cases where the inline
implementation in the CPP is causing trouble. And the more examples
the more convincing I will be.


The cases where it doesn't work should be pretty convincing.

// A.h

inline void f();

// A.cpp

#include "A.h"

inline void f()
{
}

// B.cpp

#include "A.h"

void foo()
{
f(); // linker error
}

-cd

Nov 17 '05 #5
Dirk wrote:
What about link time optimization, Carl? I thought this way a
function from another object file can be inlined.


It can. But that involves an optimization for functions that are not
declared inline, ironically enough! The basic link must first succeed
before cross module inlining is even considered.

-cd
Nov 17 '05 #6
Sharon wrote:
Usually it is common to write the class member function in the class H file,
but some people like to write the function body in the C++ file.

Can anybody tell me what are the cases where inline function should not be
written in the C++ file? Or what are the disadvantages of inline function
body in a C++ file?

A function, member of a class or not, should be written as inline when
it is small and called frequently and so we can avoid the time cost of
continuous function calls with minimal impact on the produced binary size.
Now VS 2003 unfortunately creates everything as inlined, basically
ignoring the optimisation abilities that C++ provides making it to be
like VC# 2003.
Under this view, any such large inlined function that is called more
than once, produces significant binary size impact in comparison to the
non-inlined definition, and thus should be made a regular definition
(that is non-inlined) by hand.

--
Ioannis Vranos
Nov 17 '05 #7

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

Similar topics

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...
4
by: Tony Johansson | last post by:
Hello experts! I'm reading a book about C++ and there is something about inline that the book says that is unclear for me. The book says the following "Because inline functions are expanded at...
6
by: kceiw | last post by:
How to make a member function inline while the declaration and implementation are separated?
9
by: Bilgehan.Balban | last post by:
Hi, If I define an inline function in one .c file, and use it from another, after compiling and linking the two, it seems the function is not inlined but rather called as a regular function. I...
7
by: Wu Shaohua | last post by:
Hi Guys, 1. As we know usually we should not define a constructor as inline. I also learned if we define a member function inside the class this member function will be automatically be...
9
by: chinu | last post by:
hi all, i did a small experiment to grasp the advantages of declaring a function as inline. inline int fun1(); int main(){ unsigned int start=0,end=0; asm("rdtsc\n\t"
2
by: aaragon | last post by:
Hi everyone, I would like to create a very simple function: // header file inline void point_map() { PointMap pointMap = get(vertex_point_t(), g); } // main.cpp
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.