概述
编译期运算就是指在编译阶段由编译器所进行的运算,不占用运行期时间,有时也称模版元编程。
提示:constexpr在C++11被引入。建议把编译器版本开的尽可能高。
编译期常量
编译器的一些工作,如为数组分配空间、模板特化等,依赖于一些特定的参数(数组大小,模板元素类型)才能完成。很显然的,你不能像在运行期那样轻易的向程序中传入参数,而是需要用一些常量与变量进行运算。
字面量是编译期常量。
1 2
   | int a[114514]; vector<int> b;
 
  | 
 
constexpr变量可以被用作编译期常量。
constexpr变量必须满足下列要求:
实例
编译期计算素数表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
   | #include <array> #include <iostream>
  constexpr auto get_state() {     constexpr int N = 100;     constexpr std::array<int, N> is_not_prime = []() {         std::array<int, N> is_not_prime{};         for (int i = 2; i * i < N; ++i)         {             if (!is_not_prime[i])             {                 for (int j = i * i; j < N; j += i)                 {                     is_not_prime[j] = true;                 }             }         }         return is_not_prime;     }();
      constexpr std::size_t M = [is_not_prime]() {         std::size_t cnt = 0;         for (std::size_t i = 2; i < N; ++i)         {             if (!is_not_prime[i])             {                 ++cnt;             }         }         return cnt;     }();
      std::array<int, M> state{};     for (std::size_t i = 2, j = 0; i < N; ++i)     {         if (!is_not_prime[i])         {             state[j++] = i;         }     }     return state; }
  int main() {     constexpr auto state = get_state();     for(auto i:state)         std::cout << i << ' ';
      return 0; }
 
  |