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

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 2831
"Srini" <sr*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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::ArcVersion(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*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.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******@godzilla.net> wrote in message
news:3a*************@individual.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
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>...
21
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...
1
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...
5
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...
6
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...
5
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
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...
2
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...
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.