Inline Functions

From RAD Studio
Jump to: navigation, search

Go Up to Classes Index

You can declare a member function within its class and define it elsewhere. Alternatively, you can both declare and define a member function within its class, in which case it is called an inline function.

The compiler can sometimes reduce the normal function call overhead by substituting the function call directly with the compiled code of the function body. This process, called an inline expansion of the function body, does not affect the scope of the function name or its arguments. Inline expansion is not always possible or feasible. The inline specifier indicates to the compiler you would like an inline expansion.

Note: The compiler can ignore requests for inline expansion.

Explicit and implicit inline requests are best reserved for small, frequently used functions, such as the operator functions that implement overloaded operators. For example, the following class declaration of func:

 int i;                          // global int
 class X {
 public:
    char* func(void) { return i; }  // inline by default
    char* i;
 };

is equivalent to:

 inline char* X::func(void) { return i; }

func is defined outside the class with an explicit inline specifier. The value i returned by func is the char* i of class X (see Member scope).

Inline Functions and Exceptions

An inline function with an exception-specification will never be expanded inline by the compiler. For example,

 inline void f1() throw(int)
 {
    // Warning: Functions with exception specifications are not expanded inline
 }

The remaining restrictions apply only when destructor cleanup is enabled.

Note: Destructors are called by default. See Setting exception handling options for information about exception-handling switches.

An inline function that takes at least one parameter that is of type 'class with a destructor' will not be expanded inline. Note that this restriction does not apply to classes that are passed by reference.

Example:

 struct foo {
    foo();
    ~foo();
 };
 inline void f2(foo& x)
 {
    // no warning, f2() can be expanded inline
 }
 inline void f3(foo x)
 {
    // Warning: Functions taking class-by-value argument(s) are
    //          not expanded inline in function f3(foo)
 }

An inline function that returns a class with a destructor by value will not be expanded inline whenever there are variables or temporaries that need to be destructed within the return expression:

 struct foo {
    foo();
    ~foo();
 };
 inline foo f4()
 {
    return foo();
    // no warning, f4() can be expanded inline
 }
 inline foo f5()
 {
    foo X;
    return foo(); // Object X needs to be destructed
    // Warning: Functions containing some return statements are
    //          not expanded inline in function f5()
 }
 inline foo f6()
 {
    return ( foo(), foo() );  // temporary in return value
    // Warning:Functions containing some return statements are
    //         not expanded inline in function f6()
 }

See Also