c++ inline in .cpp and not in header

Explanation of using inline in .cpp file and not in header file in C++

Inline functions

In C++, the inline keyword is used to suggest the compiler to replace the function call with the actual code of the function. Inline functions are typically defined in header files and are used to improve performance by reducing the function call overhead. However, there are cases where it is preferable to define inline functions in the .cpp file instead of the header file.

Defining inline functions in .cpp file

When you define an inline function in a .cpp file, you are essentially telling the compiler to include the function code directly at the call site, similar to how a macro would work. This means that every time the function is called, the entire code of the function is inserted at that location in the .cpp file.

Advantages of defining inline functions in .cpp file

  1. Encapsulation: By defining inline functions in the .cpp file, you can hide the implementation details of the function from the users of the header file. This can help improve the encapsulation of your code and make it easier to maintain and modify the implementation without affecting the users of the header file.

  2. Reduced compilation time: When you define an inline function in a header file, every time that header file is included in other source files, the function code is copied into those source files. This can increase the compilation time, especially if the function code is large or complex. By defining the inline function in the .cpp file, you reduce the amount of code that needs to be copied, which can help improve compilation times.

  3. Separation of interface and implementation: By moving the inline function definitions to the .cpp file, you separate the interface (defined in the header file) from the implementation (defined in the .cpp file). This can make your code easier to read and understand, as the users of the header file only need to see the function declaration and not the implementation details.

Considerations for defining inline functions in .cpp file

  1. Function visibility: When you define an inline function in a .cpp file, it is only visible within that .cpp file. If you need to use the inline function in other .cpp files, you will need to declare it in a header file and include that header file in those .cpp files.

  2. Inlining decisions: The decision of whether a function is actually inlined or not is ultimately up to the compiler. While using the inline keyword suggests to the compiler that the function should be inlined, the compiler may choose to ignore this suggestion in certain cases. Inlining decisions are influenced by factors such as the size and complexity of the function, the optimization settings of the compiler, and the target platform.

  3. Multiple definitions: If you define an inline function in a .cpp file and include that .cpp file in multiple translation units, you will end up with multiple definitions of the same function. This violates the One Definition Rule (ODR) and will result in a linker error. To avoid this, you can use the static keyword to make the function definition internal to the translation unit.

Example:

Here is an example that demonstrates how to define an inline function in a .cpp file:

// header.h
#ifndef HEADER_H
#define HEADER_H

void inlineFunction();

#endif

// source.cpp
#include "header.h"

inline void inlineFunction()
{
    // Function implementation
}

// main.cpp
#include "header.h"

int main()
{
    inlineFunction(); // Function call
    return 0;
}

In this example, the inlineFunction is defined in the source.cpp file instead of the header file. The function declaration is included in the header file, which allows other source files (in this case, main.cpp) to use the function.