How C++ moves calculations from run time to compile time
This article introduces the knowledge of "how C++ moves computing from runtime to compile time". Many people will encounter this dilemma in the operation of actual cases. next, let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
Per.11: move calculations from run time to compile time
Reason (reason)
To reduce code size and execution time. Avoid data competition through constants. To catch errors at compile time (while eliminating error handling code)
Example (sample)
Double square (double d) {return dumped;}
Static double S2 = square (2); / / old-style: dynamic initialization
Constexpr double ntimes (double d, int n) / / assume 0