In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
这篇文章主要讲解了"怎么使用C++11-20常量表达式",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"怎么使用C++11-20常量表达式"吧!
C++98时代
C++98编译器对int常量情有独钟,因为这是少数它能直接识别的东西。因为这个有限的能力,编译器就能够预先判定数组的大小了:
TEST_METHOD(TestConstVar) { //int n = 3; const int n = 3; int a[n] = { 0 }; Assert::AreEqual(size_t(3), _countof(a)); const int m = n * 3; int b[m] = { 0 }; Assert::AreEqual(size_t(9), _countof(b)); }
并由此还引入了一个"常量折叠"的概念,即编译器会自动将所有const int变量的引用全部替换为常量:
TEST_METHOD(TestConstVarFold) { const int a = 10; int b = 2 * a; int* p = (int*)&a; *p = 100; // 没有常量折叠? Assert::AreEqual(100, a); Assert::AreEqual(20, b); Assert::AreEqual(100, *p); }
我们不必纠结于这里的a到底是10还是100,这完全取决于编译器的实现。而实际工作中谁要写出这样的代码,直接拖出去打死了事。
C++11时代
constexpr值
C++98编译器对常量的那点有限智商实在是令人着急。C++11干脆就引入了一个新的关键字constexpr,以便让编译器可以做更多的事情。
TEST_METHOD(TestConstExprVar) { constexpr int n = 3; int a[n] = { 0 }; Assert::AreEqual(size_t(3), _countof(a)); constexpr int m = n * 3; int b[m] = { 0 }; Assert::AreEqual(size_t(9), _countof(b)); }
constexpr看起来和const没啥区别嘛?但实际上,你可以把constexpr理解为真正的编译期常量,而const实际上是运行期常量,以前之所以能在编译期起作用完全是不得已的救场客串行为。
constexpr函数
当然,如果constexpr仅仅有这点作用,那是绝对不会被作为新的关键字引入的。更为重要的是,既然编译期已经知道constexpr就代表编译期可以运行的东西,那么它为什么不可以修饰函数?让只能在运行期调用的函数可以在编译期起作用:
static constexpr int size() { return 3; } static constexpr int sqrt(int n) { return n * n; } static constexpr int sum(int n) { return n > 0 ? n + sum(n - 1) : 0; } TEST_METHOD(TestConstExprFunc) { int a[size()] = { 0 }; Assert::AreEqual(size_t(3), _countof(a)); int b[sqrt(3)] = { 0 }; Assert::AreEqual(size_t(9), _countof(b)); int c[sum(3)] = { 0 }; Assert::AreEqual(size_t(6), _countof(c)); }
当然,在C++11阶段,这种constexpr函数限制很多:
函数必须返回一个值,不能是void
函数体只能有一条语句return
函数调用前必须被定义
函数必须用constexpr声明
浮点型常量
尽管有些限制,但是毕竟也是个函数,所以要实现C++98编译期头疼的浮点型常量也变得很简单了:
static constexpr double pi() { return 3.1415926535897; } TEST_METHOD(TestConstExprDouble) { int a[(int)pi()] = { 0 }; Assert::AreEqual(size_t(3), _countof(a)); }
constexpr类
C++的一大特点就是面向对象的,既然constexpr可以修饰函数了,那为什么不能修饰成员函数呢?
class N { private: int m_n; public: constexpr N(int n = 0) :m_n(n) { } constexpr int getN() const { return m_n; } }; TEST_METHOD(TestConstExprConstruct) { constexpr N n(3); int a[n.getN()] = { 0 }; Assert::AreEqual(size_t(3), _countof(a)); }C++14时代
C++11的constexpr很好,很强大。但是最为令人诟病的就是constexpr函数限制实在是太多了。于是C++14开始为其松绑:
static constexpr int abs(int n) { if (n > 0) { return n; } else { return -n; } } static constexpr int sumFor(int n) { int s = 0; for (int i = 1; i = 0) { double r = 1.0, p = b; unsigned u = unsigned(n); while (u != 0) { if (u & 1) r *= p; u /= 2; p *= p; } return r; } else { return std::pow(b, double(n)); } } TEST_METHOD(TestConstEvaluated) { constexpr double p = power(3, 2); Assert::AreEqual(9.0, p, 0.001); int m = 2; Assert::AreEqual(9.0, power(3, m), 0.001); }感谢各位的阅读,以上就是"怎么使用C++11-20常量表达式"的内容了,经过本文的学习后,相信大家对怎么使用C++11-20常量表达式这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
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.