|
| 1 | +\subsection[inline]{Inline keyword} |
| 2 | + |
| 3 | +\begin{frame}[fragile] |
| 4 | + \frametitlecpp[98]{Inline keyword} |
| 5 | + \begin{block}{Inline functions originally} |
| 6 | + \begin{itemize} |
| 7 | + \item applies to a function to tell the compiler to inline it |
| 8 | + \begin{itemize} |
| 9 | + \item i.e. replace function calls by the function's content |
| 10 | + \item similar to a macro |
| 11 | + \end{itemize} |
| 12 | + \item only a hint, compiler can still choose to not inline |
| 13 | + \item avoids function call overhead |
| 14 | + \begin{itemize} |
| 15 | + \item but may increase executable size |
| 16 | + \end{itemize} |
| 17 | + \end{itemize} |
| 18 | + \end{block} |
| 19 | + \begin{exampleblock}{Major side effect} |
| 20 | + \begin{itemize} |
| 21 | + \item the linker reduces the duplicated functions into one |
| 22 | + \item an inline function definition can thus live in an header files |
| 23 | + \end{itemize} |
| 24 | + \end{exampleblock} |
| 25 | + \begin{block}{} |
| 26 | + \begin{cppcode*}{} |
| 27 | + inline int mult(int a, int b) { |
| 28 | + return a * b; |
| 29 | + } |
| 30 | + \end{cppcode*} |
| 31 | + \end{block} |
| 32 | +\end{frame} |
| 33 | + |
| 34 | +\begin{frame}[fragile] |
| 35 | + \frametitlecpp[98]{Inline keyword} |
| 36 | + \begin{block}{Inline functions nowadays} |
| 37 | + \begin{itemize} |
| 38 | + \item compilers can judge far better when to inline or not |
| 39 | + \begin{itemize} |
| 40 | + \item thus primary purpose is gone |
| 41 | + \end{itemize} |
| 42 | + \item putting functions into headers became main purpose |
| 43 | + \item many types of functions are marked \mintinline{cpp}{inline} by default: |
| 44 | + \begin{itemize} |
| 45 | + \item class member functions |
| 46 | + \item function templates |
| 47 | + \item \mintinline{cpp}{constexpr} functions |
| 48 | + \end{itemize} |
| 49 | + \end{itemize} |
| 50 | + \end{block} |
| 51 | +\end{frame} |
| 52 | + |
| 53 | +\begin{frame}[fragile] |
| 54 | + \frametitlecpp[17]{Inline keyword} |
| 55 | + \begin{block}{Inline variables} |
| 56 | + \begin{itemize} |
| 57 | + \item a global (or \mintinline{cpp}{static} member) variable specified as \mintinline{cpp}{inline} |
| 58 | + \item same side effect, linker merges all occurrences into one |
| 59 | + \item allows to define global variables/constants in headers |
| 60 | + \end{itemize} |
| 61 | + \end{block} |
| 62 | + \begin{block}{} |
| 63 | + \small |
| 64 | + \begin{cppcode*}{} |
| 65 | + // global.h |
| 66 | + inline int count = 0; |
| 67 | + inline const std::string filename = "output.txt"; |
| 68 | + // a.cpp |
| 69 | + #include "global.h" |
| 70 | + int f() { return count; } |
| 71 | + // b.cpp |
| 72 | + #include "global.h" |
| 73 | + void g(int i) { count += i; } |
| 74 | + \end{cppcode*} |
| 75 | + \end{block} |
| 76 | + \begin{alertblock}{} |
| 77 | + \begin{itemize} |
| 78 | + \item Avoid global variables! Constants are fine. |
| 79 | + \end{itemize} |
| 80 | + \end{alertblock} |
| 81 | +\end{frame} |
0 commit comments