How to declare the values calculated at compile time in C++ using constexpr
This article introduces the knowledge of "how to declare the values calculated in C++ at compile time using constexpr". In the operation of actual cases, many people will encounter such a dilemma, so 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!
Con.5: use constexpr to declare values that can be calculated at compile time
Reason (reason)
Better performance, better compile-time checking, guaranteed compile-time evaluation, no possibility of race conditions.
Better performance, better compilation checking, to ensure compile-time computing, there are no competition conditions.
Example (sample)
Double x = f (2); / / possible run-time evaluation
Const double y = f (2); / / possible run-time evaluation
Constexpr double z = f (2); / / error unless f (2) can be evaluated at compile time
Enforcement (implementation recommendations)
Flag const definitions with constant expression initializers.
Tag uses a constant definition initialized by a constant expression.
That's all for "how to declare values calculated in C++ at compile time using constexpr". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!