- Modern C++:Efficient and Scalable Application Development
- Richard Grimes Marius Bancila
- 167字
- 2021-06-10 18:28:03
Inlining
If a function calculates a value that can be calculated at compile time, you can mark it on the left of the declaration with constexpr to indicate that the compiler can optimize the code by computing the value at compile time. If the function value can be calculated at compile time, it means that the parameters in the function call must be known at compile time and so they must be literals. The function must also be a single line. If these restrictions are not met, then the compiler is free to ignore the specifier.
Related is the inline specifier. This can be placed on the left of a function declaration as a suggestion to the compiler that, when other code calls the function, rather than the compiler inserting a jump to the function in memory (and the creation of a stack frame), the compiler should put a copy of the actual code in the calling function. Again, the compiler is free to ignore this specifier.