Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Inline Functions

Introduction

Example Inline Function

Inline Templated Functions

Introduction

Implementing a program as a set of functions is good from a software engineering standpoint but function calls involve execution-time overhead. This is one of the reasons the OpenGl course I teach (uisng C++) does not use prototypes.

C++ provides inline functions to help reduce functioun call overhead – especially for small functions. Placing the qualifier inline before a function’s return type in the function definition “advises” the compiler to generate a copy of the function’s code in plce (when appropriate) to avoid a functioun call.

The trade-off is that multiple copies of the function code are inserted in the program (often making the program larger) rather than there being a single copy of the function to which control is passed each time the function is called.

The compiler can ignore the inline qualifier and typically does for all but the smallest functions.

In the following example note that the complete definition of function cube appears before it is used in the program. This is necessary so that the compiler knows how to expand the function code in its inline call.

The code snipped shown below

 

inline int max(int a, int b)

{

    return a<b?b:a;

}

 

int main ()

{

    cout <<”Maximum of 10 and 20 is “<<max(10,20) <<endl;

}

 

would be seen by the compiler as something like the following

 

int main ()

{

    cout <<”Maximum of 10 and 20 is : <<(10>20 ?10 : 20)<<endl;

}

 

 

Example Inline Function

Note: 

The definition of function appears before function is called, so a function prototype is not required.

The first line of the function definition acts as the prototype.

 

#include "stdafx.h"

#include <iostream>

using namespace std;

inline double cube( const double side )

{

      return side * side * side; // calculate cube

}

int main()

{

      double sideValue;

      cout << "Enter the side length of your cube: ";

      cin >> sideValue;

      cout << "Volume of cube with side " << sideValue << " is " << cube( sideValue ) << endl;

      return 0;

}

 

Inline Templated Functions

A template function is considered to be inline if one of the following applies:

An inline function is defined in each translation unit in which it is used and has exactly the same definition in each case. Thus, the compiler generates the same function in each of the compilation units where the template function is instantiated. The compiler may also inline the function for you (inline substitution of the function body at the point of call, similar to macro substitution).

A namespace scope template function has internal linkage if it is explicitly declared static. No other template function has internal linkage. The inline function specifier does not affect the linkage of a template function. You must define a template function that has internal linkage within the compilation unit in which it is used (implicitly or explicitly instantiated) because a name that has internal linkage cannot be referred to by other names from other translation units.

The definition of a template function must be in scope (visible) at the point of instantiation. On the other hand, the only requirement for a template function to be implicitly instantiated, is that the function declaration has to be in scope at the point of instantiation.

In the Stack template class example (shown partially below), the constructor is defined inline in the class template declaration. As a result, any compilation unit that uses an instance of the Stack class will have the appropriate constructor generated as an inline function by the compiler.

 

typedef enum{tr,fl} Bool;

template <class Item, int size> class Stack

{

public:

        int operator << (Item item);  // Push operator

        int operator >> (Item& item); // Pop operator

        Stack(Bool p=fl) {top = 0;}   // Constructor defined inline

private:

        Item stack[size];      // The stack of elements

        int top;               // Index to top of stack

};