473,614 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inline class member functions

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.

If I follow this guideline for inline class member functions, even
though the class implementation would compile, I'd have linker problems
when I link this class object file with others.

Hence, as a workaround for this situation, I thought that if I take the
address of the inline member functions, the compiler would be forced to
put the inline member functions in the object files. This would take
care of the linking problems.

My doubt, however, is this - by doing so, will the compiler stop
inlining the calls to that function?

Thanks is advance...

Regards,
Srini

Jul 23 '05 #1
7 2854
"Srini" <sr*********@gm ail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
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.
Not necessarily. I've sometimes put a definition of a private inline
function into the only source file in which it's used. Of course that's
still in the same compilation unit as the declaration.
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.
I keep them in separate files if I want to switch between inline (for
release) and non-inline (for debug). For that I use 'INLINE' instead
'inline' and #define INLINE appropriately for each case, and then I include
the file containing the definitions into either the source file or the
header file based on an #ifdef.
If I follow this guideline for inline class member functions, even
though the class implementation would compile, I'd have linker problems
when I link this class object file with others.
Not if you #include the definitions file in the header file.
Hence, as a workaround for this situation, I thought that if I take the
address of the inline member functions, the compiler would be forced to
put the inline member functions in the object files. This would take
care of the linking problems.
My doubt, however, is this - by doing so, will the compiler stop
inlining the calls to that function?


Yes. If you get linker errors without that, then the compiler can't see the
inline definitions.

Here's an example of what I've sometimes done.

// Header file:
#ifndef ARCVER_H_
#define ARCVER_H_

class ArcVersion
{
public:
ArcVersion(BYTE version);
// other members
private:
BYTE m_ver;
};

#ifdef ENABLE_INL
#undef INLINE
#define INLINE inline
#include "arcver.inl "
#endif

#endif

// INL file
INLINE ArcVersion::Arc Version(BYTE version)
{
m_ver = version;
}

// CPP file
#include "arcver.h"

#ifndef ENABLE_INL
#undef INLINE
#define INLINE
#include "arcver.inl "
#endif

Just enable ENABLE_INL or not to switch between inline and non-inline.

DW

Jul 23 '05 #2
Hi David,

Thanks for your reply.

I see what you're doing in your example. My reasoning for taking the
address of the
inline member functions, as a workaround was this. Something similar to
const variables.
const vars are never allocated on the stack. Instead they are held in
the compiler symbol
table. Unless we try to take its address. Even then, the compiler would
guarantee that
the variable remains constant if we never make use of the pointer.

const int a = 100;
int *ptr = const_cast<int *>(&a);

If we never use "ptr", the compiler would still guarantee that
something like this
is flagged as an error.

a = 200; // sorry mate - can't do that!

In the same lines, inlines are also held in the compiler symbol table.
If I take the
address of that function, the compiler would put it in object file.
But, as in case of
const vars, would the compiler inline the calls to that function?

I know that const and inline are two entirely different concepts - this
was just my thought.
Can you please comment on this?

Jul 23 '05 #3
"Srini" <sr*********@gm ail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Hi David,

Thanks for your reply.

I see what you're doing in your example. My reasoning for taking the
address of the
inline member functions, as a workaround was this. Something similar to
const variables.
const vars are never allocated on the stack. Instead they are held in
the compiler symbol
table.
I believe that the compiler can implement const values however it likes.
Obviously, it makes sense for the compiler to generate code with these
values embedded in code as hard-wired constants if that is the most
efficient implementation on a given processor.
Unless we try to take its address.
Taking the address would not necessarily make any difference except where
the address is used.
Even then, the compiler would
guarantee that
the variable remains constant if we never make use of the pointer.

const int a = 100;
int *ptr = const_cast<int *>(&a);

If we never use "ptr", the compiler would still guarantee that
something like this
is flagged as an error.
Well, if you do try to modify 'a' through your pointer it is undefined
behaviour.

a = 200; // sorry mate - can't do that!
Of course. The constness of 'a' is unrelated to its address being taken
somewhere. Your pointer 'ptr' merely forces the compiler to provide an
addressable instance of 'a' somewhere. It doesn't mean that all uses of 'a'
will be forced to fetch that addressable value from memory.
In the same lines, inlines are also held in the compiler symbol table.
If I take the
address of that function, the compiler would put it in object file.
But, as in case of
const vars, would the compiler inline the calls to that function?

I know that const and inline are two entirely different concepts - this
was just my thought.
Can you please comment on this?


As I said, if you get linker errors on inline functions it means that the
compiler saw the declarations but not the definitions. I've no doubt that
the functions would not be inlined if you make the errors go away by taking
the address. I also suggest that taking the address would not necessarily
make the linker errors go away, even if they went away in your case. I'm not
sure that just because you've taken the address is it guaranteed that the
function provided for the address-of operation will be recognized by the
linker as the function to call for the inline calls for which the compiler
could not find the definitions.

BTW, I don't agree that it is better programming practice to separate
declarations and definitions than to keep them together. I would think that
most programmers keep inline declarations and definitions in the same header
file, and I don't see a problem with that. Do you?

DW

Jul 23 '05 #4
Hi David,

Thanks for all your comments.
BTW, I don't agree that it is better programming practice to separate declarations and definitions than to keep them together. I would think that most programmers keep inline declarations and definitions in the same header file, and I don't see a problem with that. Do you?


Yes - Now I tend to agree with you. I dont see much problems with it.
:-)

Thanks again.

Regards,
Srini

Jul 23 '05 #5


David White wrote:

BTW, I don't agree that it is better programming practice to separate
declarations and definitions than to keep them together. I would think that
most programmers keep inline declarations and definitions in the same header
file, and I don't see a problem with that. Do you?


Actually we keep the inline definitions seperate from the declarations
for the same reason that you mentioned (release vs debug builds).
However, in general though we found that inline methods are a
botheration that does not merit their usage. Except for our very
low-level geometry classes (points, vectors, workplanes) we've yet to
see a profile report that has indicated that inlining would result in a
measurable speed improvement.

Jul 23 '05 #6

Srini wrote:
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.


Yes. It's called a trade-off. "inline" makes maintenance harder but
performance higher. So, use in on the functions that really matter.

HTH,
Michiel Salters

Jul 23 '05 #7
"lilburne" <li******@godzi lla.net> wrote in message
news:3a******** *****@individua l.net...
David White wrote:

BTW, I don't agree that it is better programming practice to separate
declarations and definitions than to keep them together. I would think that most programmers keep inline declarations and definitions in the same header file, and I don't see a problem with that. Do you?


Actually we keep the inline definitions seperate from the declarations
for the same reason that you mentioned (release vs debug builds).
However, in general though we found that inline methods are a
botheration that does not merit their usage. Except for our very
low-level geometry classes (points, vectors, workplanes) we've yet to
see a profile report that has indicated that inlining would result in a
measurable speed improvement.


The usefuleness of inlined functions depends very much on the application.
In my spare time for the last year or so I've been working on a program for
solving Pocoman puzzles (http://www.sleepless.com/pocoman). The execution
times of the optimized-for-speed release build for one case are:
~10 seconds (inline "any suitable", in VC++ 6.0)
~25 seconds (inline "Disable")

DW
Jul 23 '05 #8

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

Similar topics

7
2688
by: Senthilraja | last post by:
I have the following program using templates. Someone please let me know the syntax to be used for defining the member functions push, pop etc. as non-inline functions. #include <iostream> using namespace std; template<class T, int size = 50> class Stack {
21
746
by: Rubén Campos | last post by:
I haven't found any previous message related to what I'm going to ask here, but accept my anticipated excuses if I'm wrong. I want to ask about the real usefulness of the 'inline' keyword. I've read many authors (and it's my belief, too) who discourage the use of the 'inline' keyword, because: - The 'inline' word only advice the compiler about wich functions should be expanded, but not force it to expand them. Also, the compiler can...
1
1605
by: kelvSYC | last post by:
I'm not sure if it violates standards or if it's just one of gcc's quirks, but on gcc with Mac OS X (at least with my installation), for some reason inline member functions that are not part of the class declaration always produce linker errors. For example, take a piece of my code: // random access iterator for Cocoa Foundation's NSArray // technically obj-c++, but only the c++ part that is relevant class NSArrayIterator :...
5
1922
by: ganesh.gella | last post by:
Hi All, Wanted to make sure on this, I have some functions for which I am providing the definition inside the class definition. (As these are very small functions like get and set). Do i need to declare these functions as inline (I would like them to be inlined) ? or are these functions considered for inline by default as
6
2447
by: John Ratliff | last post by:
I was reading the C++ FAQ Lite about inline functions, and it says the following (http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.7) " It's usually imperative that the function's definition (the part between the {...}) be placed in a header file. If you put the inline function's definition into a .cpp file, and if it is called from some other .cpp file, you'll get an "unresolved external" error from the linker. " Why...
5
4280
by: Martin Vorbrodt | last post by:
so if i have this: // header.h class X { public: static int s = 1; }; i still need this:
7
16094
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...
2
3766
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the common meanings: static as in "statically allocated" as opposed to on the stack or on the free store and static as in "with restricted visibility" as opposed to with external linkage. For member functions, static has the second meaning."
17
8365
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
8182
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
8130
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
8627
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
8579
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8433
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
7093
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
6088
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...
1
2568
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
0
1425
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.