The Wayback Machine - https://web.archive.org/web/20130723005109/http://zh.cppreference.com:80/w/cpp/language/constexpr

constexpr specifier (C++11 起)

来自cppreference.com

 
 
C + +语言
大会的主题
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
流量控制
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
条件执行语句
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
迭代语句
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
跳转语句
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
功能
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
函数声明
lambda函数的声明
函数模板
的历史。内嵌说明
异常规范 (过时了)
noexcept说明 (C++11)
例外
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
命名空间
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
类型
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
规范
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CV符
存储时间说明符
constexpr说明 (C++11)
汽车符 (C++11)
alignas说明 (C++11)
初始化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
布尔文字
nullptr (C++11)
用户定义的 (C++11)
表达式
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
另一种表示形式
实用工具
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
类型
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
声明类型别名 (C++11)
属性 (C++11)
施放
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
隐式转换
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-风格和功能转换
内存分配
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
类特定的功能特性
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
虚函数
覆盖说明 (C++11)
最后说明 (C++11)
明确的 (C++11)
静态的
特殊的成员函数
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
模板
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
类模板
函数模板
模板特化
参数包 (C++11)
杂项
Original:
Miscellaneous
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 - specifies that the value of a variable or function can be computed at compile time
    The 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.
“”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.
  • 必须立即建造或分配一个值.
    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, constexpr variables 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 of constexpr constructor. 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.
  • 它不能是虚拟的
    Original:
    it must not be virtual
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 它的返回类型必须是LiteralType
    Original:
    its return type must be LiteralType
    The 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 type
    The 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 statements
    The 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 declarations
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • typedef声明和别名声明没有定义的类或枚举
    Original:
    typedef declarations and 别名 declarations that do not define classes or enumerations
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 使用声明
    Original:
    using declarations
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • using指令
    Original:
    using directives
    The 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 one return statement that contains only literal values, constexpr variables 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.
  • 每个参数都必须是文本类型
    Original:
    each of its parameters must be literal type
    The 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 classes
    The 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 statements
    The 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 declarations
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • typedef声明和别名声明没有定义的类或枚举
    Original:
    typedef declarations and 别名 declarations that do not define classes or enumerations
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 使用声明
    Original:
    using declarations
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • using指令
    Original:
    using directives
    The 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 block
    The 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 the initializer_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
    The 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 expression
    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:

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.

#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