In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to distinguish between C++ constant expression, const, constexpr, the article is very detailed, has a certain reference value, interested friends must read it!
A constant expression is an expression whose value does not change and can be evaluated at compile time.
Example 1:
# include using namespace std;int main () {const int A1 = 10; / / A1 is a constant expression. Const int a2 = A1 + 20; / / a2 is the constant expression int A3 = 5; / / a3 is not the constant expression const int A4 = A3; / / A4 is not a constant expression, because the execution of the A3 program is initialized when it reaches its declaration, so the value of the variable A4 is not known until the program runs. But the compilation is fine! Return 0;}
The above code can be compiled normally.
It shows that what const declares is not necessarily a constant expression!
The new standard Category 11 allows variables to be declared as constexpr types so that the compiler can verify whether the value of the variable is a constant expression. The constexpr specifier declaration can obtain the value of a function or variable at compile time. A variable declared as constexpr must be a constant and must be initialized with a constant expression.
Example 2:
# include using namespace std;int main () {const int A1 = 10; / / A1 is a constant expression. Const int a2 = A1 + 20; / / a2 is the constant expression int A3 = 5; / / a3 is not the constant expression constexpr int A4 = A3; / / A4 is not a constant expression, because the execution of the A3 program is initialized when it reaches its declaration, so the value of the variable A4 is not known until the program runs. Compiler error! Return 0;}
Constexpr int A4 = A3; compilation will report an error!
Example 3:
# include using namespace std;int main () {const int A1 = 10; / / A1 is a constant expression. Const int a2 = A1 + 20; / / a2 is the constant expression int A3 = 5; / / a3 is not the constant expression const int A4 = A3; / / A4 is not a constant expression, because the execution of the A3 program is initialized when it reaches its declaration, so the value of the variable A4 is not known until the program runs. Compiler error! Char arr1 [a2]; / / No problem char arr2 ['y']; / / No problem, the ASCII code of'y' is 121, which is equivalent to char arr2 [121l]; char arr3 [A4]; / / compile error, because A4 is not constant expression return 0;} above is all the content of this article "how to distinguish C++ constant expression, const, constexpr", thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.