Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / November 2004

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sharon - 25 Nov 2004 15:19 GMT
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?

Signature

Regards
Sharon G.

Carl Daniel [VC++ MVP] - 25 Nov 2004 15:35 GMT
> 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++
[quoted text clipped - 3 lines]
> 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
Sharon - 25 Nov 2004 15:47 GMT
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
Carl Daniel [VC++ MVP] - 25 Nov 2004 16:21 GMT
> 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
[quoted text clipped - 3 lines]
> 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
Dirk - 27 Nov 2004 22:34 GMT
What about link time optimization, Carl? I thought this way a function from
another object file can be inlined.

>> Hi Carl,
>>
[quoted text clipped - 40 lines]
>
> -cd
Carl Daniel [VC++ MVP] - 28 Nov 2004 01:16 GMT
> 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
Ioannis Vranos - 28 Nov 2004 06:42 GMT
> 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.

Signature

Ioannis Vranos


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.