473,796 Members | 2,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use Inline in class?

I want to know if the practice is the best. Do I need to place inline
keyword inside class definition or outside member function
definition. For example

class A
{
public:
A();
~A();

inline void Test(); // should place inline here?
};

inline void A::Test() // should place inline here?
{
{
Jul 6 '08
32 2094
James Kanze wrote:
But globally, all of the
coding guidelines I've seen for application code forbid inline
functions---and usually also require user defined
implementations of the functions the compiler will generate by
default as well, since the compiler generated versions are
inline.
Where's the problem with inline compiler generated functions?

--
Ian Collins.
Jul 7 '08 #11
On Jul 6, 9:59 pm, "Alf P. Steinbach" <al...@start.no wrote:
* Gennaro Prota:
Alf P. Steinbach wrote:
* James Kanze:
[...]
If you do actually have a performance problem, and the
profiler shows that it is in a tight loop where you do
call a non-inlined function, then inlining it is a simple
and cheap optimization. Using inline before the profiler
says you have to, however, is premature optimization.
Inlining things in headers is a technique that saves
programmer's time both for initial development, for use of
the header, and for maintainance (which reportedly
constitutes about 80% of all programming work).
I think that if this is really your issue (hardly, but I'll
assume you have some measurements) then you (or your users
in the case e.g. of libraries, for which I've often heard
the "header-only is easier" claim) are likely to work with a
severely limited environment and toolset. To me, creating
one more file is just a matter of issuing ":e filename" and
the file contents get initialized automatically with all
that can be written automatically. Similarly for building.
It's also easy to write Perl. ;-)
But we both agree that maintenance is an important issue:-).
As to maintenance I really don't understand: what time do
you save?
For things that are defined in the header file you only need
to look at one file, affecting continuity, and you see the
relevant definition in context.
There are some arguments in favor of "all in the header", in
certain conditions. (If you're distributing a library over
multiple platforms, for example, it certainly makes the
distribution easy.) For the most part, however, code isn't (and
shouldn't be) designed as a monolithic block. Before attacking
the maintenance, you read the documentation, to understand the
basic role of the class in the context of the application. From
the documentation and the test results, you should be able to
determine directly which function is misbehaving (otherwise,
your tests aren't adequately precise), and usually, even, have a
fair idea as to why it is misbehaving. So you don't need
authorization to check out the header; you can just check out
the implementation file for the function, make the correction,
and be done with it. Even in less perfect situations (and I'll
admit, not every organization runs as smoothly as I just
described), you'll generally have to look at several different
classes, in several different headers, before deciding which
implementation file needs working on. And in those classes
which you don't want to modify (usually the majority), you don't
want to have to search through tons of implementation code to
find the relevant declarations; Java without (well written)
Javadoc is unmaintainable, but you can actually produce readable
class definitions in C++. (Even with inline functions---the
inline functions will normally be defined outside of the class
anyway.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 7 '08 #12
On Jul 7, 10:11 am, Ian Collins <ian-n...@hotmail.co mwrote:
James Kanze wrote:
But globally, all of the
coding guidelines I've seen for application code forbid inline
functions---and usually also require user defined
implementations of the functions the compiler will generate by
default as well, since the compiler generated versions are
inline.
Where's the problem with inline compiler generated functions?
The day you have to change them (and they cease to become
compiler generated), you have to modify the header.

In the end, implementation details don't belong in the header.
We're stuck with regards to private data and functions (although
the compilation firewall can limit the problems), but for the
rest, you only put what is necessary for the client in the
header. The implementation of a function, or even the fact that
the implementation is compiler generated, is an implementation
detail.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 7 '08 #13
On Jul 7, 11:47 am, "Alf P. Steinbach" <al...@start.no wrote:
* James Kanze:
[...]
This sounds very much like arbitrary decisions that have made
themselves into law, and are not being rationalizated is if
they were meaningful.
Actually, it's all based on concrete experience.

If you prefer to ignore concrete experience, that's your
privilege.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 7 '08 #14
James Kanze wrote:
On Jul 7, 10:11 am, Ian Collins <ian-n...@hotmail.co mwrote:
>James Kanze wrote:
>>But globally, all of the
coding guidelines I've seen for application code forbid inline
functions---and usually also require user defined
implementatio ns of the functions the compiler will generate by
default as well, since the compiler generated versions are
inline.
>Where's the problem with inline compiler generated functions?

The day you have to change them (and they cease to become
compiler generated), you have to modify the header.
I'd expect the reason a class would require an explicit version of a
compiler generated function would be a change to its data members.

I guess it all comes back to the old discussion about build times. I'm
used to an environment where changing headers is a normal part of the
development process.
In the end, implementation details don't belong in the header.
I can't argue with that.
We're stuck with regards to private data and functions (although
the compilation firewall can limit the problems), but for the
rest, you only put what is necessary for the client in the
header. The implementation of a function, or even the fact that
the implementation is compiler generated, is an implementation
detail.
Yes and no - it is a detail, but it's a hidden detail.

--
Ian Collins.
Jul 7 '08 #15
James Kanze wrote:
In the end, implementation details don't belong in the header.
We're stuck with regards to private data and functions (although
the compilation firewall can limit the problems), but for the
rest, you only put what is necessary for the client in the
header. The implementation of a function, or even the fact that
the implementation is compiler generated, is an implementation
detail.
Note that in C++0x we should finally be able to require
compiler-generation of special member functions without making them
inline:

<http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm>

--
Gennaro Prota | <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
Jul 7 '08 #16
On Jul 7, 10:13 pm, Ian Collins <ian-n...@hotmail.co mwrote:
James Kanze wrote:
On Jul 7, 10:11 am, Ian Collins <ian-n...@hotmail.co mwrote:
James Kanze wrote:
But globally, all of the coding guidelines I've seen for
application code forbid inline functions---and usually
also require user defined implementations of the functions
the compiler will generate by default as well, since the
compiler generated versions are inline.
Where's the problem with inline compiler generated
functions?
The day you have to change them (and they cease to become
compiler generated), you have to modify the header.
I'd expect the reason a class would require an explicit
version of a compiler generated function would be a change to
its data members.
You've never added logging or event notifications?

Typically, of course, you're right, and the compiler generated
inlines are generally less of a problem than user defined ones.
In fact, the main reason given for avoiding them was code bloat;
in the case of constructors and destructors, the apparently
"empty" function can generate a lot of code.

In my own code (i.e. code where I don't have to follow
externally imposed guidelines), I vary somewhat, using common
sense and my knowledge of the class' semantics: I'd certainly
not bother defining the compiler generated defaults for
something like complex, for example, where as if the class has
some real, application level behavior, I probably would.

And of course, if the code is a template, the rules are
definitelyl relaxed, because the source level coupling is
already there anyway.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 8 '08 #17
James Kanze wrote:
>In-lining functions in a header file can be critical for
performance reasons.

Really? I never use it, and I've not had any performance
problems.
I think you are underestimating the importance of inlining with
respect to speed (and even executable size).

For example, a small program I made to test the speed of a (very
template-heavy) memory allocator library I made, runs in 16 seconds, and
the executable is 10kB (stripped).

If I add the compiling option -fno-inline, which stops gcc from
inlining any function, all the other optimizations being the same, the
program now runs in 1 minute 11 seconds, and the executable is 18kB in size.

Not only does inlining make the program a lot faster, it even makes it
considerably smaller (debunking the common misconception most people
have about inlining making executables bigger).
Jul 8 '08 #18
On Mon, 07 Jul 2008 18:26:56 -0700, Greg Herlihy wrote:
On Jul 7, 1:16Â*am, James Kanze <james.ka...@gm ail.comwrote:
>On Jul 6, 9:59 pm, "Alf P. Steinbach" <al...@start.no wrote: Java
without (well written)
[...]
C++, of course
requires that a class interface include protected and private methods
and members. Note that - even though these methods and members are not
"accessible " - they nonetheless are "visible", and can therefore cause
senseless naming conflicts and access violations for clients of the
interface.
Huh? Example?

--
Lionel B
Jul 8 '08 #19
James Kanze wrote:
On Jul 7, 10:13 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>James Kanze wrote:
>>On Jul 7, 10:11 am, Ian Collins <ian-n...@hotmail.co mwrote:
James Kanze wrote:
But globally, all of the coding guidelines I've seen for
applicati on code forbid inline functions---and usually
also require user defined implementations of the functions
the compiler will generate by default as well, since the
compiler generated versions are inline.
>>>Where's the problem with inline compiler generated
functions?
>>The day you have to change them (and they cease to become
compiler generated), you have to modify the header.
>I'd expect the reason a class would require an explicit
version of a compiler generated function would be a change to
its data members.

You've never added logging or event notifications?

Typically, of course, you're right, and the compiler generated
inlines are generally less of a problem than user defined ones.
In fact, the main reason given for avoiding them was code bloat;
in the case of constructors and destructors, the apparently
"empty" function can generate a lot of code.
Doesn't the standard require at most one copy in the entire program of a
non-static inline function? I'm assuming compiler generated functions
fall into this category. Or do they?

--
Ian Collins.
Jul 8 '08 #20

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

Similar topics

2
10120
by: Jeff Williams | last post by:
The common method of defining template classes and functions is to put the definition and declaration into the same header file. Or at least I believe it to be the common method and it is certainly the one I use. This leaves me with a question. As example, consider the following class. template<class T> class foo
23
3162
by: Mat | last post by:
<div id="container"> <div id="main"> <div id="header"> <p class="Address">123 Fake Street, </p> <p class="City">Crazy City, </p> <p class="Province">Ontario </p> <p class="PostalCode">H0H 0H0</p> <p class="Telephone">Telephone: 555-1234 </p> <p class="Fax">Fax: 555-4321</p> </div>
20
3157
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
2863
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.
7
2037
by: Alvin | last post by:
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 {
6
4009
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
7
16127
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 inline'ed. My question is: If I define a constructor (including its body) or another large member function inside the class, the constructor or the member function is inline or not? why? 2. I learned that if the member function is big we should not...
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() {}
17
8386
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 the function if possible" (because if the compiler has the function definition available at an instantiation point, it will estimate whether to inline it or not, and do so if it estimates it would be beneficial, completely regardless of whether...
0
9680
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
9528
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,...
1
10173
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10006
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
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...
0
6788
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4116
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
3
2925
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.