FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


C++ An Inline Question





umeshtangnu
Hi
I have a little problem regarding inline functions ,does declaring a function inside the class definition make it inline or not.
qscomputing
No, a function is only inline if you add the inline keyword to it.

Inline has nothing to do with functionality; it is simply a note to the compiler to write the function code out every time it sees a call to it, rather than putting one copy of the function and jumping to it every time. This sacrifices size for a slightly faster program.
Indi
umeshtangnu wrote:
Hi
I have a little problem regarding inline functions ,does declaring a function inside the class definition make it inline or not.

Well that's actually a trickier question than it first appears to be. The common but wrong answer is "yes". The correct but too short answer is "maybe".

But first, some housekeeping:
qscomputing wrote:
No, a function is only inline if you add the inline keyword to it.

False. There is more than one way to make a function inline. The inline keyword is not always necessary.

qscomputing wrote:
Inline has nothing to do with functionality; it is simply a note to the compiler to write the function code out every time it sees a call to it, rather than putting one copy of the function and jumping to it every time. This sacrifices size for a slightly faster program.

False. Inlining a function may make the program faster, or it may make it slower. It may make the program bigger, or it may make it smaller. There is no simple rule to determine what will happen. It requires experience with the processor being targetted, and even then, it's only valid for a certain combination of processor and compiler. And even then, you'll still never know unless you profile.

--------------

Unless you use some compiler specific commands (like "forceinline"), telling the compiler to inline a function is just a suggestion. The compiler is free to honour it, or ignore it. It is also free to inline other functions that you didn't ask to be inlined.

There are basically two (normal) ways to inline a function.

1.) Explicitly, with the inline keyword:
Code:
// in header file
inline void foo()
{
  // You have requested foo() be inlined
}

class bar
{
public:
   void baz();
};

inline void bar::baz()
{
   // You have requested that bar::baz() gets inlined
}


2.) Implicitly in a class declaration:
Code:
class foo
{
public:
   void bar()
   {
      // You have requested that foo::bar() gets inlined
   }
};


In all cases, the compiler is free to ignore your request. The compiler is also free to inline other functions, as it sees fit.

--------------------

Now you're probably scratching your head. Inlining may make your code faster or slower, bigger or smaller, and hey, the compiler is free to do its own thing anyway. So... what's a programmer to do?

Trust your compiler. The good ones are smart enough now to figure what to do on their own. And if you're compiler isn't... then, frankly, you're using the wrong compiler. Because if performance matters that much to you, you can control freak over your code all you want but a shitty compiler will still only frustrate your efforts. And if it's so critical that even a proper optimizing compiler still isn't good enough... then, frankly, you're using the wrong language. You should be using assembly code.

For virtually all practical purposes, even a half-decent optimizing compiler is plenty good enough these days. Just let it do its thing.

Course it doesn't hurt to give it hints. There are some good places to ask for inlining, and if you're interested, i'll share some of them with you.
Indi
qscomputing wrote:
Inline has nothing to do with functionality; it is simply a note to the compiler....

Oh, i forgot to mention about this part...

False.

Making a function inline or outline does change its functionality in several complex and devious ways. Most are platform specific, but some are not.

For example:
Code:
// If the compiler were actually to honour the inline command without question:

int foo(int a)
{
  return a * 2;
}

inline int bar(int a)
{
  return a * 2;
}

int array[] = { 1, 2, 3, 4, 5 };

transform(array, array + sizeof(array), array, ptr_fun(foo)); // No problems
transform(array, array + sizeof(array), array, ptr_fun(bar)); // Won't work

That is why the compiler is free to ignore the inline request. All compilers will in that situation, using varying methods.
Related topics
[Question] Visual Basic C++, PHP, mySQL language guides...
An interesting test...
C++ Question
IP Class C portioning
question aux codeurs ! php noob InsiDe!!
How to make a Html post in Phpnuke
[C#] Safe place to store information
Custom encryption + program
la vie des etudiants noir en russie
C > Chr() in C++??
A C program in Linux
Quick Question in C language
A confusion with C# and VB.NET...
Cocoa/Objective-C NSPipe question
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.