constexpr specifier (C++11 起)
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
constexpr
- 指定的变量或函数的值可以在编译时计算原文:constexpr
- specifies that the value of a variable or function can be computed at compile time
[编辑] 解释
constexpr
说明声明,它是可能的,以评估值的函数或变量在编译的时候,他们可以只编译常量表达式允许的时间。 constexpr意味着const.原文:
constexpr
specifier declares that it is possible to evaluate the value of the function or variable at compile time, they then can be used where only compile time 常量表达式 are allowed. constexpr implies const.“”constexpr变量必须满足以下要求:1
原文:
constexpr variables must satisfy the following requirements:
- 必须立即建造或分配一个值.原文:it must be immediately constructed or assigned a value.
- 构造函数的参数或值将被分配的值必须只包含文字,
constexpr
变量和函数.原文:the constructor parameters or the value to be assigned must contain only literal values,constexpr
variables and functions. - 使用构造函数来构造对象(隐式或显式)的
constexpr
构造必须满足的要求。在显式构造函数的情况下,它必须constexpr指定.原文:the constructor used to construct the object (either implicit or explicit) must satisfy the requirements ofconstexpr
constructor. In the case of explicit constructor, it must have constexpr specified.
-
constexpr功能“'必须满足以下要求:1
原文:
constexpr functions must satisfy the following requirements:
- 它不能是虚拟的
- 它的返回类型必须是
LiteralType
- 每个参数都必须是文本类型原文:each of its parameters must be literal type
- 函数体必须予以删除或拖欠或只包含以下原文:the function body must be either deleted or defaulted or contain only the following:
-
constexpr构造函数必须满足下列要求:
原文:
constexpr constructor must satisfy the following requirements:
- 每个参数都必须是文本类型原文:each of its parameters must be literal type
- 类必须有虚基类原文:the class must have no virtual base classes
- 构造函数体必须删除或拖欠或只包含以下原文:the constructor body must be either deleted or defaulted or contain only the following:
- 空语句
- static_assert声明
- 使用声明
- using指令
-
- 构造函数不能有函数的try块原文:the constructor must not have a function-try block
- 每一个基类,每一个非静态成员必须初始化,无论是在
initializer_list
或大括号或等于初始值设定项。结束每个构造函数必须是一个constexpr构造函数和每一个括号或等于初始值设定项的每个条款都必须是一个常量表达式原文:every base class and every non-static member must be initialized, either in theinitializer_list
or by brace-or-equal initializer. End every constructor involved must be a constexpr constructor and every clause of every brace-or-equal initializer must be a constant expression - 每一个隐式转换,必须是一个常量表达式原文:every implicit conversion involved must be a constant expression
-
[编辑] 关键字
[编辑] 示例
定义的计算阶乘和文字类型的扩展字符串文字的constexpr功能
原文:
Definition of a constexpr function which computes factorials and a literal type that extends string literals
#include <iostream> #include <stdexcept> // constexpr functions use recursion rather than iteration constexpr int factorial(int n) { return n <= 1 ? 1 : (n * factorial(n-1)); } // literal class class conststr { const char * p; std::size_t sz; public: template<std::size_t N> constexpr conststr(const char(&a)[N]) : p(a), sz(N-1) {} // constexpr functions signal errors by throwing exceptions from operator ?: constexpr char operator[](std::size_t n) { return n < sz ? p[n] : throw std::out_of_range(""); } constexpr std::size_t size() { return sz; } }; constexpr std::size_t countlower(conststr s, std::size_t n = 0, std::size_t c = 0) { return n == s.size() ? c : s[n] >= 'a' && s[n] <= 'z' ? countlower(s, n+1, c+1) : countlower(s, n+1, c); } // output function that requires a compile-time constant, for testing template<int n> struct constN { constN() { std::cout << n << '\n'; } }; int main() { std::cout << "4! = " ; constN<factorial(4)> out1; // computed at compile time volatile int k = 8; std::cout << k << "! = " << factorial(k) << '\n'; // computed at run time std::cout << "Number of lowercase letters in \"Hello, world!\" is "; constN<countlower("Hello, world!")> out2; // implicitly converted to conststr }
输出:
4! = 24 8! = 40320 Number of lowercase letters in "Hello, world!" is 9