值得注意的是,inline 只是一个 request to complier,至于complier到底会不会一定接受,那是不一定的。(通常编译器不会inline一个递归函数
constexpr funcs are implicitly inline
constexpr Functions
顾名思义,这是一个可以作为“constant expression”的函数
the return type and the type of each parameter in a must be a literal.
the function body must contain exactly one return statement.\
Not Require to Return a constexpr Expression
反直觉?其实不管是para还是return,都可以是普通变量,关键在于是否是编译时刻就决定的
constexpr size_t scale(size_t cnt) { return new_sz() * cnt; }int arr[scale(2)]; // ok: scale(2) is a constant expression int i = 2; // i is not a constant expressionint a2[scale(i)]; // error: scale(i) is not a constant expression
Put in Header Files
Unlike other functions, inline and constexpr functions may be defined multiple times in the program. As a result, inline and constexpr functions normally are defined in headers.