📖
Go C++
  • Introduction
  • Chapter 1: What You Must Know First
    • Virtual Address Space of Process: Memory Partition and Layout
    • Function Call: Stack Frame
    • Program Compiling and Linking
  • Chapter 2: C++ Basics Improvement
    • Default Parameters
    • Inline Function
    • Function Overloading
    • new and delete
    • const and Pointers
    • References in Detail
  • Chapter 3: Object-Oriented Principles
  • Class and Object
  • Constructor and Destructor
  • Shallow Copy and Deep Copy
  • Initializer List
  • Various Member Functions
  • Pointer to Class Members
  • Chapter 4: Template Programming
  • Function Templates
  • Class Templates
  • Memory Allocators
  • Chapter 5: Operator Overloading
    • Operator Overloading
    • Introduction to Iterators
    • Issues of Iterator Invalidation
    • More about new and delete
    • Overloading of new and delete: Object Pool
  • Chapter 6: Inheritance and Polymorphism
    • Look inside Inheritance
    • More about Inheritance
    • Virtual Functions, Static Binding and Dynamic Binding
    • More about Virtual Functions
    • Understanding Polymorphism
    • Abstract Classes
    • Frequently Asked Interview Questions: Polymorphism
  • Chapter 7: Multiple Inheritance
    • Virtual Inheritance and Virtual Base Classes
    • Diamond Problem
    • Four Kinds of Type Conversions
  • Chapter 8: Standard Template Library
    • Sequence Containers
    • Container Adaptors
    • Associative Containers
    • More about Iterators
    • Function Objects
    • Generic Algorithms, Binders and Lambda Expressions
  • Chapter 9: Object Optimization
    • Behind the Object
    • Optimizing Objects in Functions
    • Member Functions with Rvalue References
    • Move Semantics and Perfect Forwarding
  • Chapter 10: Smart Pointers
    • Smart Pointers
    • Smart Pointers without Reference Counting
    • Smart Pointers with Reference Counting
    • Custom Deleters
  • Chapter 11: Function Objects and Binders
    • More about Binders
    • Introduction to std::function
    • Template Specialization and Argument Deduction
    • More about std::function
    • std::bind(): A Simple Thread Pool
    • More about Lambda Expressions
  • Chapter 12: Multithreading
    • Important Features in C++11
    • Multithreaded Programming with std::thread
    • Mutual Exclusion
    • Producer-Consumer Problem
    • Atomic Operations
    • Thread Visibility and volatile
  • Chapter 13: Design Patterns
    • Singleton Pattern
    • Factory Pattern
    • Proxy Pattern
    • Decorator Pattern
    • Adapter Pattern
    • Observer Pattern
Powered by GitBook
On this page

Was this helpful?

  1. Chapter 2: C++ Basics Improvement

Inline Function

Let's look at the following program

int sum(int x, int y) {
    return x + y;
}
​
int main() {
    int a = 10;
    int b = 20;
    int ret = sum(a, b);
    return 0;
}

When main() calls sum(), it needs to push all parameters into stack frame, create a new stack frame for sum(), and return the stack frame back to system after it quits. But what if we call sum() thousands of times? Notice that sum() function only involves three operations (mov, add, and mov), then it is a huge amount of overhead in calling the function comparing to the calculation itself. Under this circumstances, we had better mark function sum() as inline.

An function with inline simply does one work: it expand all the commands inside the function, and insert them into where the function is called, during the compiling process. In this way, the function calling process is eliminated.

inline int sum(int x, int y) {
    return x + y;
}
​
int main() {
    int a = 10;
    int b = 20;
    int ret = sum(a, b);    // Convert into: int ret = a + b;
    return 0;
}

Since there's no function call, there's no need to generate function symbols inside the symbol table. To verify this, we can compile this code with O2 compiler optimization as g++ -c main.cpp -O2 and dump the object file with objdump -t main.o.

main.o:     file format elf64-x86-64
​
SYMBOL TABLE:
0000000000000000 l    df *ABS*  0000000000000000 main.cpp
0000000000000000 l    d  .text  0000000000000000 .text
0000000000000000 l    d  .data  0000000000000000 .data
0000000000000000 l    d  .bss   0000000000000000 .bss
0000000000000000 l    d  .text.startup  0000000000000000 .text.startup
0000000000000000 l    d  .note.GNU-stack        0000000000000000 .note.GNU-stack
0000000000000000 l    d  .eh_frame      0000000000000000 .eh_frame
0000000000000000 l    d  .comment       0000000000000000 .comment
0000000000000000 g     F .text.startup  0000000000000003 main

However, not every function marked with inline will be converted into inline function. A recursive function, for example, which calls itself constantly, can not be inlined because the times it is called is only known at runtime. Moreover, if the function has too many lines, it may also not be inlined.

Therefore, the keyword inline is just a suggestion for the compiler to make it inlined. It is up to the compiler to decide whether make it inlined or not.

In VS Code, inline only works in the release version. The debug version makes it unavailable in order to facilitate the debugging of the program.

PreviousDefault ParametersNextFunction Overloading

Last updated 4 years ago

Was this helpful?