0

i'm confused about how to do inline functions in C++....

lets say this function. how would it be turned to an inline function

int maximum( int x, int y, int z )
{
   int max = x;
   if ( y > max )
      max = y;
   if ( z > max )
      max = z;
   return max;
}   
user69514
  • 26,935
  • 59
  • 154
  • 188
  • 7
    Put "inline" in front of the function definition. –  Apr 29 '10 at 16:36
  • 3
    You'd also have to put the function definition in a header (or otherwise make it available in each translation unit... – Michael Burr Apr 29 '10 at 16:43
  • 3
    Bear in mind that the `inline` keyword usually has no effect on whether a function is inlined or not, with any reasonably modern compiler. What it will do is allow you to define the function in multiple places, as long as all the definitions are identical (e.g., you could have that defined in multiple headers if it were `inline`). – David Thornley Apr 29 '10 at 16:44
  • You may also do `return (x > y ? (x > z ? x : z) : (y > z ? y : z));`. Maybe that's what was intended by the question? – Johannes Schaub - litb Apr 29 '10 at 16:45
  • 1
    You could also make it a function template, which might be more reliable than using `inline`. – John Dibling Apr 29 '10 at 16:45
  • @litb: This is hard to read. Rises too many questions. I'd suggest a [potentially] branchless `x * (x >= y && x >= z) + y * (y > x && y >= z) + z * (z > x && z > y)` instead :) – AnT stands with Russia Apr 29 '10 at 16:57
  • How about std::max(x, std::max(y, z)) – Firas Assaad Apr 29 '10 at 17:03
  • Or for something this simple, just make it a macro... – zdav Apr 29 '10 at 18:06

6 Answers6

8

In order to turn it into an inline function you need to do two things:

  1. Declare it inline by using keyword inline.
  2. Make sure that the definition of this function is visible in every translation unit where it is used. This normally means that you have to put the entire definition of the function into a header file.
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
4

To post Neils answer as an actual answer:

inline int maximum(int x, int y, int z)
....
John Weldon
  • 39,849
  • 11
  • 94
  • 127
4

As others have said, you can use the inline keyword to tell the compiler you want your function inlined. But the inline keyword is just a compiler hint. The compiler can and will chose to ignore your request if it wants or needs to.

An alternative is to make your function a function template, which will often be blown out inline:

template<class Val>
Val maximum( Val x, Val y, Val z )
{
   Val max = x;
   if ( y > max )
      max = y;
   if ( z > max )
      max = z;
   return max;
}   
John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • 1
    Function templates are not necessarily expanded inline. – sbi Apr 29 '10 at 16:52
  • 2
    Niether are `inline` functions. This is just an alternative approach. – John Dibling Apr 29 '10 at 16:55
  • 1
    Andrey's answer is better… being a template has no bearing whatsoever on `inline` or inlining. It's just a coincidence that templates and inline functions tend to be put in header files. Andrey explains the point… better not to encourage pointless templatization. – Potatoswatter Apr 29 '10 at 19:19
3

If that function definition appears inside a class {} definition, then it is automatically inline already.

Otherwise, as others say, put inline infront.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
2

To make the function inline use the inline keyword:

inline int maximum( int x, int y, int z ) // note the inline keyword
{
   int max = x;
   if ( y > max )
      max = y;
   if ( z > max )
      max = z;
   return max;
}

If the function is a member of a class/struct then simply defining it inside the class (as apposed to outside it) makes it inline.

Say you have the call:

int f = maximum(3, 4, 5)

The compiler might expands the call to something like:

int max = x;
if ( y > max )
   max = y;
if ( z > max )
   max = z;
int z = max;

There's some overhead to calling a function, so inline functions give you the convenience of functions along with the performance of C macros. But that's not to say you should always use them, in most cases the compiler is better at deciding when optimizations like this are needed and might not even honor your request.

You can read more about inline functions and how (and when) to use them at C++ FAQ Lite and this GotW

Firas Assaad
  • 25,006
  • 16
  • 61
  • 78
1

inline just tells the compiler that you want the function code copied everywhere it is refernece, it makes the code a bit faster (no function call overhead) but bigger (the code is copied). This page is more in depth.

zdav
  • 2,752
  • 17
  • 15
  • And the compiler is not required to actually inline it if it doesn't feel like it. So it may or may not help. – Zan Lynx Apr 29 '10 at 16:56