constexpr 说明符 (C++11)
C++11 标准引入了新的关键字
constexpr 作为声明说明符。 只能将 constexpr 说明符应用于以下上下文:- 变量的定义
- 函数或函数模板的声明
- 静态数据成员的声明
constexpr int i = 1; // OK, definition
constexpr extern int j; // Error, not a definition
constexpr int f1(); // OK, function declaration, but must be defined before use
如果您声明的函数不是具有
constexpr 说明符的构造函数,那么该函数是 constexpr 函数。 同样,如果使用 constexpr 说明符声明构造函数,那么该构造函数是 constexpr 构造函数。 constexpr 函数和 constexpr 构造函数都隐式内联。 例如:struct S {
constexpr S(int i) : mem(i) { } // OK, declaration of a constexpr constructor
private:
int mem;
};
constexpr S s(55); // OK, invocation of a constexpr constructor
constexpr 说明符对 constexpr 函数或 constexpr 构造函数的类型没有影响。 如果使用 constexpr指定了函数或函数模板的任何声明,那么其所有声明都必须包含 constexpr 说明符。 例如:constexpr int f1(); // OK, function declaration
int f1() { // Error, the constexpr specifier is missing
return 55;
} 无法使用
constexpr 说明符声明函数参数。 以下示例对此进行了演示:constexpt int f4(constexpr int); //Error在对象声明中使用的
constexpr 说明符将对象声明为 const。 此类对象必须是字面值类型且已初始化。 如果它由构造函数调用初始化,那么该调用必须是常量表达式。 否则,如果在引用声明中使用 constexpr 说明符,那么在其初始化方法中出现的每个完整表达式都必须是常量表达式。 转换初始化方法表达式时使用的每个隐式转换以及用于初始化的每个构造函数调用都必须在常量表达式中有效。 例如:constexpr int var; // Error, var is not initialized
constexpr int var1 = 1; // OK
void func() {
var1 = 5; //Error, var1 is const
}
struct L {
constexpr L() : mem(55) { }
constexpr L(double d) : mem((int)d) { }
L(int i) : mem(i) { }
operator int() { return mem; }
private:
int mem;
};
// Error, initializer involves a non-constexpr constructor.
constexpr L var2(55);
double var3 = 55;
// Error, initializer involves a constexpr constructor with non-constant argument
constexpr L var4(var3);
// Error, involves conversion that uses a non-constexpr conversion function
constexpr int var5 = L(); 不是构造函数的非静态成员函数的
constexpr 说明符将该成员函数声明为 const。 该 constexpr 成员函数的类必须是文字类型。 在以下示例中,类 NL 是非文字类型,因为它具有用户提供的析构函数。struct NL {
constexpr int f(){ //error, enclosing class is not a literal type
return 55;
}
~NL() { }
};
对 constexpr 函数的调用会生成与对等效非constexpr 函数的调用相同的结果,但对 constexpr 函数的调用可能出现在常量表达式中。
无法使用 constexpr 说明符声明 main 函数。