constexpr specifier (C++11 起)
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
constexpr- 指定的变量或函数的值可以在编译时计算Original:constexpr- specifies that the value of a variable or function can be computed at compile timeThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 解释
constexpr说明声明,它是可能的,以评估值的函数或变量在编译的时候,他们可以只编译常量表达式允许的时间。 constexpr意味着const.Original:
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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
“”constexpr变量必须满足以下要求:1
Original:
constexpr variables must satisfy the following requirements:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- 必须立即建造或分配一个值.Original:it must be immediately constructed or assigned a value.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 构造函数的参数或值将被分配的值必须只包含文字,
constexpr变量和函数.Original:the constructor parameters or the value to be assigned must contain only literal values,constexprvariables and functions.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 使用构造函数来构造对象(隐式或显式)的
constexpr构造必须满足的要求。在显式构造函数的情况下,它必须constexpr指定.Original:the constructor used to construct the object (either implicit or explicit) must satisfy the requirements ofconstexprconstructor. In the case of explicit constructor, it must have constexpr specified.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
constexpr功能“'必须满足以下要求:1
Original:
constexpr functions must satisfy the following requirements:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- 它不能是虚拟的Original:it must not be virtualThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 它的返回类型必须是
LiteralTypeOriginal:its return type must beLiteralTypeThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 每个参数都必须是文本类型Original:each of its parameters must be literal typeThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 函数体必须予以删除或拖欠或只包含以下Original:the function body must be either deleted or defaulted or contain only the following:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- 空语句Original:null statementsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - static_assert声明Original:static_assert declarationsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 使用声明Original:using declarationsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - using指令Original:using directivesThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 一个
return只包含文字值,constexpr变量和函数的声明,..Original:exactly onereturnstatement that contains only literal values,constexprvariables and functions.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
-
constexpr构造函数必须满足下列要求:
Original:
constexpr constructor must satisfy the following requirements:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- 每个参数都必须是文本类型Original:each of its parameters must be literal typeThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 类必须有虚基类Original:the class must have no virtual base classesThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 构造函数体必须删除或拖欠或只包含以下Original:the constructor body must be either deleted or defaulted or contain only the following:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- 空语句Original:null statementsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - static_assert声明Original:static_assert declarationsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 使用声明Original:using declarationsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - using指令Original:using directivesThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
- 构造函数不能有函数的try块Original:the constructor must not have a function-try blockThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 每一个基类,每一个非静态成员必须初始化,无论是在
initializer_list或大括号或等于初始值设定项。结束每个构造函数必须是一个constexpr构造函数和每一个括号或等于初始值设定项的每个条款都必须是一个常量表达式Original:every base class and every non-static member must be initialized, either in theinitializer_listor 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 expressionThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 每一个隐式转换,必须是一个常量表达式Original:every implicit conversion involved must be a constant expressionThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
[编辑] 关键字
[编辑] 为例
定义的计算阶乘和文字类型的扩展字符串文字的constexpr功能
Original:
Definition of a constexpr function which computes factorials and a literal type that extends string literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#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 }
Output:
4! = 24 8! = 40320 Number of lowercase letters in "Hello, world!" is 9

